8072767: DefaultCellEditor for comboBox creates ActionEvent with wrong source object
authoralexsch
Wed, 15 Apr 2015 14:38:13 +0400
changeset 30463 06e0b7759125
parent 30462 507bcb03c954
child 30464 929bafd0db6f
8072767: DefaultCellEditor for comboBox creates ActionEvent with wrong source object Reviewed-by: serb, azvegint
jdk/src/java.desktop/share/classes/javax/swing/JComboBox.java
jdk/test/javax/swing/JComboBox/8072767/bug8072767.java
--- a/jdk/src/java.desktop/share/classes/javax/swing/JComboBox.java	Tue Apr 14 15:43:14 2015 -0700
+++ b/jdk/src/java.desktop/share/classes/javax/swing/JComboBox.java	Wed Apr 15 14:38:13 2015 +0400
@@ -577,6 +577,8 @@
                 if (!found) {
                     return;
                 }
+
+                getEditor().setItem(anObject);
             }
 
             // Must toggle the state of this flag since this method
@@ -1319,16 +1321,12 @@
      * do not call or override.
      */
     public void actionPerformed(ActionEvent e) {
-        ComboBoxEditor editor = getEditor();
-        if ((editor != null) && (e != null) && (editor == e.getSource()
-                || editor.getEditorComponent() == e.getSource())) {
-            setPopupVisible(false);
-            getModel().setSelectedItem(editor.getItem());
-            String oldCommand = getActionCommand();
-            setActionCommand("comboBoxEdited");
-            fireActionEvent();
-            setActionCommand(oldCommand);
-        }
+        setPopupVisible(false);
+        getModel().setSelectedItem(getEditor().getItem());
+        String oldCommand = getActionCommand();
+        setActionCommand("comboBoxEdited");
+        fireActionEvent();
+        setActionCommand(oldCommand);
     }
 
     /**
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/JComboBox/8072767/bug8072767.java	Wed Apr 15 14:38:13 2015 +0400
@@ -0,0 +1,111 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ /*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.awt.Point;
+import java.awt.Rectangle;
+import java.awt.Robot;
+import java.awt.event.InputEvent;
+import java.awt.event.KeyEvent;
+import javax.swing.DefaultCellEditor;
+import javax.swing.JComboBox;
+import javax.swing.JFrame;
+import javax.swing.JTable;
+import javax.swing.SwingUtilities;
+
+/**
+ * @test
+ * @bug 8072767
+ * @author Alexander Scherbatiy
+ * @summary DefaultCellEditor for comboBox creates ActionEvent with wrong source
+ *          object
+ * @run main bug8072767
+ */
+
+public class bug8072767 {
+
+    private static final String TEST1 = "Test";
+    private static final String TEST2 = TEST1 + 1;
+
+    private static JFrame frame;
+    private static JTable table;
+    private static Point point;
+    private static boolean testPass;
+
+    public static void main(String[] args) throws Exception {
+        Robot robot = new Robot();
+        robot.setAutoDelay(50);
+        SwingUtilities.invokeAndWait(bug8072767::createAndShowGUI);
+        robot.waitForIdle();
+        SwingUtilities.invokeAndWait(() -> {
+            point = table.getLocationOnScreen();
+            Rectangle rect = table.getCellRect(0, 0, true);
+            point.translate(rect.width / 2, rect.height / 2);
+        });
+        robot.mouseMove(point.x, point.y);
+        robot.mousePress(InputEvent.BUTTON1_MASK);
+        robot.mouseRelease(InputEvent.BUTTON1_MASK);
+        robot.waitForIdle();
+
+        robot.keyPress(KeyEvent.VK_1);
+        robot.keyRelease(KeyEvent.VK_1);
+        robot.waitForIdle();
+
+        SwingUtilities.invokeAndWait(() -> {
+            point = frame.getLocationOnScreen();
+            point.translate(frame.getWidth() / 2, frame.getHeight() / 2);
+        });
+
+        robot.mouseMove(point.x, point.y);
+        robot.mousePress(InputEvent.BUTTON1_MASK);
+        robot.mouseRelease(InputEvent.BUTTON1_MASK);
+        robot.waitForIdle();
+
+        SwingUtilities.invokeAndWait(() -> {
+            testPass = TEST2.equals(table.getValueAt(0, 0));
+            frame.dispose();
+        });
+
+        if (!testPass) {
+            throw new RuntimeException("Table cell is not edited!");
+        }
+    }
+
+    private static void createAndShowGUI() {
+        frame = new JFrame();
+        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+        frame.setSize(200, 200);
+        frame.setLocation(100, 100);
+
+        table = new JTable(
+                new String[][]{{TEST1}}, new String[]{"Header"});
+        JComboBox<String> comboBox = new JComboBox<>();
+        comboBox.setEditable(true);
+        table.getColumnModel().getColumn(0).setCellEditor(
+                new DefaultCellEditor(comboBox));
+        frame.getContentPane().add(table);
+        frame.setVisible(true);
+    }
+}