Path: blob/master/test/jdk/javax/swing/JTable/4220171/bug4220171.java
41153 views
/*1* Copyright (c) 2012, 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 422017127* @author Konstantin Eremin28* @summary Tests29* @library ../../regtesthelpers30* @build Util31* @run main bug422017132*/33import java.awt.Color;34import java.awt.Point;35import java.awt.Rectangle;36import java.awt.Robot;37import java.awt.event.InputEvent;38import java.awt.event.KeyEvent;39import javax.swing.*;40import javax.swing.border.LineBorder;4142public class bug4220171 {4344private static JTable table;45private static JFrame frame;4647public static void main(String args[]) throws Exception {48try {4950Robot robot = new Robot();51robot.setAutoDelay(50);5253javax.swing.SwingUtilities.invokeAndWait(new Runnable() {5455public void run() {56createAndShowGUI();57}58});5960robot.waitForIdle();6162clickMouse(robot, 0, 0);63Util.hitKeys(robot, KeyEvent.VK_A, KeyEvent.VK_B, KeyEvent.VK_ENTER);64robot.waitForIdle();65checkCell(0, 0);6667clickMouse(robot, 0, 1);68Util.hitKeys(robot, KeyEvent.VK_D, KeyEvent.VK_E, KeyEvent.VK_ENTER);69robot.waitForIdle();70checkCell(0, 1);7172clickMouse(robot, 1, 0);73Util.hitKeys(robot, KeyEvent.VK_1, KeyEvent.VK_2, KeyEvent.VK_ENTER);74robot.waitForIdle();75checkCell(1, 0);7677clickMouse(robot, 1, 1);78Util.hitKeys(robot, KeyEvent.VK_4, KeyEvent.VK_5, KeyEvent.VK_ENTER);79robot.waitForIdle();80checkCell(1, 1);81} finally {82if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());83}84}8586static void checkCell(final int row, final int column) throws Exception {87SwingUtilities.invokeAndWait(new Runnable() {8889public void run() {90if (table.getValueAt(row, column) != null) {91throw new RuntimeException(92String.format("Cell (%d, %d) is editable", row, column));93}94}95});96}9798static void clickMouse(Robot robot, int row, int column) throws Exception {99Point point = getCellClickPoint(row, column);100robot.mouseMove(point.x, point.y);101robot.mousePress(InputEvent.BUTTON1_MASK);102robot.mouseRelease(InputEvent.BUTTON1_MASK);103}104105private static Point getCellClickPoint(final int row, final int column) throws Exception {106final Point[] result = new Point[1];107SwingUtilities.invokeAndWait(new Runnable() {108109@Override110public void run() {111Rectangle rect = table.getCellRect(row, column, false);112Point point = new Point(rect.x + rect.width / 2,113rect.y + rect.height / 2);114SwingUtilities.convertPointToScreen(point, table);115result[0] = point;116}117});118119return result[0];120}121122private static void createAndShowGUI() {123frame = new JFrame("Test");124frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);125frame.setSize(200, 200);126127table = new JTable(2, 2);128table.setEnabled(false);129130frame.getContentPane().add(table);131frame.setVisible(true);132}133}134135136