Path: blob/master/test/jdk/javax/swing/JComboBox/8072767/bug8072767.java
41153 views
/*1* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.Point;24import java.awt.Rectangle;25import java.awt.Robot;26import java.awt.event.InputEvent;27import java.awt.event.KeyEvent;28import javax.swing.DefaultCellEditor;29import javax.swing.JComboBox;30import javax.swing.JFrame;31import javax.swing.JTable;32import javax.swing.SwingUtilities;3334/**35* @test36* @key headful37* @bug 807276738* @summary DefaultCellEditor for comboBox creates ActionEvent with wrong source39* object40* @run main bug807276741*/4243public class bug8072767 {4445private static final String TEST1 = "Test";46private static final String TEST2 = TEST1 + 1;4748private static JFrame frame;49private static JTable table;50private static volatile Point point;51private static boolean testPass;5253public static void main(String[] args) throws Exception {54Robot robot = new Robot();55robot.setAutoDelay(100);56SwingUtilities.invokeAndWait(bug8072767::createAndShowGUI);57robot.waitForIdle();58robot.delay(1000);59SwingUtilities.invokeAndWait(() -> {60point = table.getLocationOnScreen();61Rectangle rect = table.getCellRect(0, 0, true);62point.translate(rect.width / 2, rect.height / 2);63});64robot.mouseMove(point.x, point.y);65robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);66robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);67robot.waitForIdle();6869robot.keyPress(KeyEvent.VK_1);70robot.keyRelease(KeyEvent.VK_1);71robot.waitForIdle();7273SwingUtilities.invokeAndWait(() -> {74point = frame.getLocationOnScreen();75point.translate(frame.getWidth() / 2, frame.getHeight() / 2);76});7778robot.mouseMove(point.x, point.y);79robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);80robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);81robot.waitForIdle();8283SwingUtilities.invokeAndWait(() -> {84testPass = TEST2.equals(table.getValueAt(0, 0));85frame.dispose();86});8788if (!testPass) {89throw new RuntimeException("Table cell is not edited!");90}91}9293private static void createAndShowGUI() {94frame = new JFrame();95frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);96frame.setSize(200, 200);9798table = new JTable(99new String[][]{{TEST1}}, new String[]{"Header"});100JComboBox<String> comboBox = new JComboBox<>();101comboBox.setEditable(true);102table.getColumnModel().getColumn(0).setCellEditor(103new DefaultCellEditor(comboBox));104frame.getContentPane().add(table);105frame.setVisible(true);106frame.setLocationRelativeTo(null);107}108}109110111