Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8057893

JComboBox actionListener never receives "comboBoxEdited" from getActionCommand

XMLWordPrintable

    • b40
    • x86_64
    • windows_7

        FULL PRODUCT VERSION :
        java version "1.8.0_20"
        Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
        Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)

        ADDITIONAL OS VERSION INFORMATION :
        Microsoft Windows [Version 6.1.7601]

        A DESCRIPTION OF THE PROBLEM :
        (Small source code test case included)

        In jdk7 and earlier the JComboBox actionListener provided an event where event.getActionCommand was equal to "comboBoxEdited". In jdk8 it does not.

        The bug appears because of a source code change in JComboBox in jdk 8. In
        javax.swing.JComboBox.java::actionPerformed the following two lines of code
        were added in jdk8.
                ComboBoxEditor editor = getEditor();
                if ((editor != null) && (e != null) && (editor == e.getSource())) {
        This test fails in jdk8 because
            editor --> BasicComboBoxEditor
            e.getSource --> BasicComboBoxEditor$BorderlessTextField
        and so the event does not get delivered.


        -------

        Perhaps these lines were added because someone encountered multiple actionListener events for one edit. And instead of using getActionCommand() to filter the events they made this change and broke things.


        REGRESSION. Last worked in version 7u67

        ADDITIONAL REGRESSION INFORMATION:
        all version 7 work correctly

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        1) compile attached source code
        2) execute program, a JFrame with JComboBox is displayed
        3) in click in combo, input some chars, for example "three", enter RETURN
        4) there is no output, it should print the event

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        The combo box listener should get an event where
                "comboBoxEdited".equals(e.getActionCommand())
        is true
        ACTUAL -
        no output

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        package comboboxeditoreventbug;

        import java.awt.EventQueue;

        import javax.swing.JComboBox;
        import javax.swing.JFrame;
        import javax.swing.WindowConstants;

        public class ComboBoxEditorEventBug {
            /**
             * @param args the command line arguments
             */
            public static void main(String[] args)
            {
                EventQueue.invokeLater(() -> {
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                    JComboBox combo = new JComboBox(new String[] {"one", "two"});
                    combo.setEditable(true);
                    combo.addActionListener((e) -> {
                        if("comboBoxEdited".equals(e.getActionCommand()))
                            System.out.println("action: " + e);
                    });
                    frame.add(combo);
                    frame.pack();
                    frame.setVisible(true);
                });
            }

        }


        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        I do
                combo.setEditor(new MyComboBoxEditor());
        where
                MyComboBoxEditor extends BasicComboBoxEditor
        to provide MyTextField (which extends JTextField).

        MyTextField overrides actionPerformed to provide it's associated combobox as
        event.getSource instead of the text field itself.

        I believe the correct fix is to remove the lines that were added in jdk8.

              alexsch Alexandr Scherbatiy
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Created:
                Updated:
                Resolved: