jcombobox as cell editor java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location

后端 未结 4 1338
天命终不由人
天命终不由人 2021-01-19 10:20

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

4条回答
  •  粉色の甜心
    2021-01-19 11:01

    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 Runnableat 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.

提交回复
热议问题