JDK-8057893 : JComboBox actionListener never receives "comboBoxEdited" from getActionCommand
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 8u20,9
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_7
  • CPU: x86_64
  • Submitted: 2014-09-06
  • Updated: 2015-06-04
  • Resolved: 2014-10-30
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8 JDK 9
8u40Fixed 9 b40Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Description
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.


Comments
is it reproducible in 8 GA?
28-10-2014