javaswingevent-handlingjcomboboxjpopup

How to Listen for JCombobox Selection Event


When I select the JCombobox I want to handle an event when its selected and the dropdown is shown as well as handle the event when the drop down disappears and the JCombobox is de-selected.

Note, i'm not looking to listen for an item selection change but for when the user selects the JCombobox and the UI pops out the Dropdown.


Solution

  • You want to use addPopupMenuListener which uses the following interface:

    public interface PopupMenuListener extends EventListener {
    
        /**
         *  This method is called before the popup menu becomes visible 
         */
        void popupMenuWillBecomeVisible(PopupMenuEvent e);
    
        /**
         * This method is called before the popup menu becomes invisible
         * Note that a JPopupMenu can become invisible any time 
         */
        void popupMenuWillBecomeInvisible(PopupMenuEvent e);
    
        /**
         * This method is called when the popup menu is canceled
         */
        void popupMenuCanceled(PopupMenuEvent e);
    }