The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.swing  [141 examples] > JComboBox  [12 examples]

e766. Listening for Action Events from a JComboBox Component

Action events are generated whenever the selected item changes. These events are generated even while the user is moving through items in the displayed popup menu. Unlike item events (see e765 Listening for Changes to the Selected Item in a JComboBox Component), action events are generated even if the new item is the same as the old item.
    // Create component
    String[] items = {"item1", "item2"};
    JComboBox cb = new JComboBox(items);
    cb.setEditable(true);
    
    // Create and register listener
    MyActionListener actionListener = new MyActionListener();
    cb.addActionListener(actionListener);
    
    class MyActionListener implements ActionListener {
        // Retain the previously selected item in order to determine whether
        // the new item is the same
        Object oldItem;
    
        // This method is called whenever the user or program changes the selected item.
        // Note: The new item may be the same as the previous item.
        public void actionPerformed(ActionEvent evt) {
            JComboBox cb = (JComboBox)evt.getSource();
    
            // Get the new item
            Object newItem = cb.getSelectedItem();
    
            // Determine if different from previously selected item
            boolean same = newItem.equals(oldItem);
            oldItem = newItem;
    
            if ("comboBoxEdited".equals(evt.getActionCommand())) {
                // User has typed in a string; only possible with an editable combobox
            } else if ("comboBoxChanged".equals(evt.getActionCommand())) {
                // User has selected an item; it may be the same item
            }
        }
    }

 Related Examples
e756. Creating a JComboBox Component
e757. Getting and Setting the Selected Item in a JComboBox Component
e758. Getting the Items in a JComboBox Component
e759. Adding and Removing an Item in a JComboBox Component
e760. Selecting an Item in a JComboBox Component with Multiple Keystrokes
e761. Determining If the Menu of a JComboBox Component Is Visible
e762. Displaying the Menu in a JComboBox Component Using a Keystroke
e763. Displaying the Menu in a JComboBox Component Using a Keystroke If the Selected Item Is Not Unique
e764. Setting the Number of Visible Items in the Menu of a JComboBox Component
e765. Listening for Changes to the Selected Item in a JComboBox Component
e767. Determining When the Menu of a JComboBox Component Is Displayed

See also: Actions    JButton    JCheckBox    JDesktop and JInternalFrame    JFrame, JWindow, JDialog    JLabel    JList    JProgressBar    JRadioButton    JScrollPane    JSlider    JSpinner    JSplitPane    JTabbedPane    JToolBar    Keystrokes and Input Maps    Layout    Look and Feel    Menus    Progress Monitor    The Screen    Tool Tips    UI Default Values   


© 2002 Addison-Wesley.