Path: blob/master/test/jdk/javax/swing/JTable/4275046/bug4275046.java
41154 views
/*1* Copyright (c) 2014, 2020, 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*/2223/*24* @test25* @key headful26* @bug 427504627* @summary Tests editable combo box as a table editor.28* @run main bug427504629*/3031import java.awt.AWTException;32import java.awt.Point;33import java.awt.Rectangle;34import java.awt.Robot;35import java.awt.event.InputEvent;36import java.awt.event.KeyEvent;37import javax.swing.DefaultCellEditor;38import javax.swing.SwingUtilities;39import javax.swing.JComboBox;40import javax.swing.JFrame;41import javax.swing.JTable;42import javax.swing.table.DefaultTableModel;4344public class bug4275046 {4546private static final String[] colNames = { "ID", "Color", "Stuff" };47private static final Object[][] data = { { 1, "red", "abc"},48{ 2, "red", "def"},49{ 3, "red", "ghijk"} };5051private static final String EXPECTED_VALUE = "rededited";5253private JFrame frame;54private JTable table;5556private volatile Point tableLoc;57private volatile Rectangle cellRect;5859private volatile Object editedValue;60private volatile boolean testResult;6162private final Robot robot;6364public static void main(String[] args) throws Exception {65final bug4275046 test = new bug4275046();66test.test();67}6869public bug4275046() throws AWTException {70robot = new Robot();71robot.setAutoDelay(100);72}7374private void createGUI() {75frame = new JFrame("bug4275046");76frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);7778JComboBox<Object> cb = new JComboBox<>(79new Object[] {"blue", "yellow", "green", "red"});80cb.setEditable(true);81DefaultCellEditor comboEditor = new DefaultCellEditor(cb);82comboEditor.setClickCountToStart(1);8384DefaultTableModel model = new DefaultTableModel(data, colNames);85table = new JTable(model);86table.getColumnModel().getColumn(1).setCellEditor(comboEditor);8788frame.add(table);89frame.pack();90frame.setSize(550, 400);91frame.setVisible(true);92}9394private void test() throws Exception {95try {96SwingUtilities.invokeAndWait(new Runnable() {97@Override98public void run() {99createGUI();100}101});102103runTest();104checkResult();105} finally {106SwingUtilities.invokeAndWait(new Runnable() {107@Override108public void run() {109if (frame != null) {110frame.dispose();111}112}113});114}115}116117private void runTest() throws Exception {118robot.waitForIdle();119120// Click the first cell in the "color" column121SwingUtilities.invokeAndWait(new Runnable() {122@Override123public void run() {124tableLoc = table.getLocationOnScreen();125cellRect = table.getCellRect(0, 1, true);126}127});128129robot.mouseMove(tableLoc.x + cellRect.x + cellRect.width / 2,130tableLoc.y + cellRect.y + cellRect.height / 2);131robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);132robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);133robot.waitForIdle();134135// Edit the cell136robot.keyPress(KeyEvent.VK_E);137robot.keyRelease(KeyEvent.VK_E);138139robot.keyPress(KeyEvent.VK_D);140robot.keyRelease(KeyEvent.VK_D);141142robot.keyPress(KeyEvent.VK_I);143robot.keyRelease(KeyEvent.VK_I);144145robot.keyPress(KeyEvent.VK_T);146robot.keyRelease(KeyEvent.VK_T);147148robot.keyPress(KeyEvent.VK_E);149robot.keyRelease(KeyEvent.VK_E);150151robot.keyPress(KeyEvent.VK_D);152robot.keyRelease(KeyEvent.VK_D);153robot.delay(100);154155// Click another cell156SwingUtilities.invokeAndWait(new Runnable() {157@Override158public void run() {159cellRect = table.getCellRect(1, 2, true);160}161});162163robot.mouseMove(tableLoc.x + cellRect.x + cellRect.width / 2,164tableLoc.y + cellRect.y + cellRect.height / 2);165robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);166robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);167robot.delay(100);168}169170private void checkResult() throws Exception {171robot.waitForIdle();172SwingUtilities.invokeAndWait(new Runnable() {173@Override174public void run() {175// Read the edited value of from the cell176editedValue = table.getModel().getValueAt(0, 1);177System.out.println("The edited value is = " + editedValue);178testResult = editedValue.equals(EXPECTED_VALUE);179if (testResult) {180System.out.println("Test passed");181} else {182System.out.println("Test failed");183}184}185});186if (!testResult) {187throw new RuntimeException("Expected value in the cell: '" +188EXPECTED_VALUE + "' but found '" + editedValue + "'.");189}190}191}192193194