# HG changeset patch # User alexsch # Date 1429094293 -14400 # Wed Apr 15 14:38:13 2015 +0400 # Node ID 6893829312e6a5576cf24b915a7c0acdeefe388f # Parent b7be27e5e8eac3610f4960b3ed364229e1b8a834 8072767: DefaultCellEditor for comboBox creates ActionEvent with wrong source object Reviewed-by: serb, azvegint diff --git a/src/share/classes/javax/swing/JComboBox.java b/src/share/classes/javax/swing/JComboBox.java --- a/src/share/classes/javax/swing/JComboBox.java +++ b/src/share/classes/javax/swing/JComboBox.java @@ -568,6 +568,8 @@ if (!found) { return; } + + getEditor().setItem(anObject); } // Must toggle the state of this flag since this method @@ -1307,16 +1309,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); } /** diff --git a/test/javax/swing/JComboBox/8072767/bug8072767.java b/test/javax/swing/JComboBox/8072767/bug8072767.java new file mode 100644 --- /dev/null +++ b/test/javax/swing/JComboBox/8072767/bug8072767.java @@ -0,0 +1,112 @@ +/* + * 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(); + robot.delay(500); + 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 comboBox = new JComboBox<>(); + comboBox.setEditable(true); + table.getColumnModel().getColumn(0).setCellEditor( + new DefaultCellEditor(comboBox)); + frame.getContentPane().add(table); + frame.setVisible(true); + } +}