Determining When the Menu of a JComboBox Component Is Displayed

// Create component String[] items = {"item1", "item2"}; JComboBox cb = new JComboBox(items); cb.setEditable(true); // Create and register listener MyPopupMenuListener actionListener = new MyPopupMenuListener(); cb.addPopupMenuListener(actionListener); class MyPopupMenuListener implements PopupMenuListener { // This method is called just before the menu becomes visible public void popupMenuWillBecomeVisible(PopupMenuEvent evt) { JComboBox cb = (JComboBox)evt.getSource(); } // This method is called just before the menu becomes hidden public void popupMenuWillBecomeInvisible(PopupMenuEvent evt) { JComboBox cb = (JComboBox)evt.getSource(); } // This method is called when menu is hidden because the user cancelled it public void popupMenuCanceled(PopupMenuEvent evt) { JComboBox cb = (JComboBox)evt.getSource(); } }

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.