I am using a custom JComboBox as a cell editor in a JTable. When the users gets to the cell using keyboard controls it tries to open the popup. This causes the following e
Popups like the pulldown in a JComboBox tend to have edge cases for event handling order because they are not geometrically nested inside their ancestors in the component hierarchy. In your case, you are causing the box's focus handler to show the pulldown. To do this it needs the box to be already located on the screen, but it's not.
The solution is almost certainly to defer showing the pulldown until all the events that will make the box visible have been processed. I had a similar (though not exactly the same) problem and was able to resolve it in this manner. Happily there is a Swing utility function that does the trick. Try wrapping the body of the focus gained handler in invokeLater
and a Runnable
:
void focusGained() {
SwingUtilities.invokeLater(new Runnable() {
... focus gained body including show of pulldown menu here ...
});
}
The invokeLater
puts a new message containing the Runnable
at the end of the queue, i.e. after all the existing ones. That Runnable
is executed only when the message makes it to the head, after all those other messages have been processed. This is exactly what you want.