Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JTable/4220171/bug4220171.java
38854 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* @bug 422017126* @author Konstantin Eremin27* @summary Tests28* @library ../../regtesthelpers29* @build Util30* @run main bug422017131*/32import java.awt.Color;33import java.awt.Point;34import java.awt.Rectangle;35import java.awt.Robot;36import java.awt.event.InputEvent;37import java.awt.event.KeyEvent;38import javax.swing.*;39import javax.swing.border.LineBorder;4041public class bug4220171 {4243private static JTable table;4445public static void main(String args[]) throws Exception {4647Robot robot = new Robot();48robot.setAutoDelay(50);4950javax.swing.SwingUtilities.invokeAndWait(new Runnable() {5152public void run() {53createAndShowGUI();54}55});5657robot.waitForIdle();5859clickMouse(robot, 0, 0);60Util.hitKeys(robot, KeyEvent.VK_A, KeyEvent.VK_B, KeyEvent.VK_ENTER);61robot.waitForIdle();62checkCell(0, 0);6364clickMouse(robot, 0, 1);65Util.hitKeys(robot, KeyEvent.VK_D, KeyEvent.VK_E, KeyEvent.VK_ENTER);66robot.waitForIdle();67checkCell(0, 1);6869clickMouse(robot, 1, 0);70Util.hitKeys(robot, KeyEvent.VK_1, KeyEvent.VK_2, KeyEvent.VK_ENTER);71robot.waitForIdle();72checkCell(1, 0);7374clickMouse(robot, 1, 1);75Util.hitKeys(robot, KeyEvent.VK_4, KeyEvent.VK_5, KeyEvent.VK_ENTER);76robot.waitForIdle();77checkCell(1, 1);78}7980static void checkCell(final int row, final int column) throws Exception {81SwingUtilities.invokeAndWait(new Runnable() {8283public void run() {84if (table.getValueAt(row, column) != null) {85throw new RuntimeException(86String.format("Cell (%d, %d) is editable", row, column));87}88}89});90}9192static void clickMouse(Robot robot, int row, int column) throws Exception {93Point point = getCellClickPoint(row, column);94robot.mouseMove(point.x, point.y);95robot.mousePress(InputEvent.BUTTON1_MASK);96robot.mouseRelease(InputEvent.BUTTON1_MASK);97}9899private static Point getCellClickPoint(final int row, final int column) throws Exception {100final Point[] result = new Point[1];101SwingUtilities.invokeAndWait(new Runnable() {102103@Override104public void run() {105Rectangle rect = table.getCellRect(row, column, false);106Point point = new Point(rect.x + rect.width / 2,107rect.y + rect.height / 2);108SwingUtilities.convertPointToScreen(point, table);109result[0] = point;110}111});112113return result[0];114}115116private static void createAndShowGUI() {117JFrame frame = new JFrame("Test");118frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);119frame.setSize(200, 200);120121table = new JTable(2, 2);122table.setEnabled(false);123124frame.getContentPane().add(table);125frame.setVisible(true);126}127}128129130