|
| 1 | +/* |
| 2 | + * Copyright (c) 1999, 2024, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +import java.awt.Point; |
| 25 | +import java.awt.Rectangle; |
| 26 | +import java.awt.Robot; |
| 27 | +import java.awt.event.InputEvent; |
| 28 | +import java.awt.event.KeyEvent; |
| 29 | + |
| 30 | +import javax.swing.JFrame; |
| 31 | +import javax.swing.JTable; |
| 32 | +import javax.swing.SwingUtilities; |
| 33 | +import javax.swing.UIManager; |
| 34 | + |
| 35 | +/* |
| 36 | + * @test |
| 37 | + * @bug 4222153 |
| 38 | + * @key headful |
| 39 | + * @summary Verify that when tab is pressed, the focus shift to next row first cell, |
| 40 | + * if the current selected cell is the last cell in that row. |
| 41 | + * @run main bug4222153 |
| 42 | + */ |
| 43 | + |
| 44 | +public class bug4222153 { |
| 45 | + private static JFrame frame; |
| 46 | + private static JTable table; |
| 47 | + private static volatile Point tableLoc; |
| 48 | + private static volatile Rectangle cellRect; |
| 49 | + private static volatile int selectedRowBeforeTabPress; |
| 50 | + private static volatile int selectedColumnBeforeTabPress; |
| 51 | + private static volatile int selectedRowAfterTabPress; |
| 52 | + private static volatile int selectedColumnAfterTabPress; |
| 53 | + |
| 54 | + public static void main(String[] args) throws Exception { |
| 55 | + UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); |
| 56 | + Robot robot = new Robot(); |
| 57 | + robot.setAutoDelay(50); |
| 58 | + try { |
| 59 | + SwingUtilities.invokeAndWait(bug4222153::createAndShowUI); |
| 60 | + robot.waitForIdle(); |
| 61 | + robot.delay(1000); |
| 62 | + |
| 63 | + SwingUtilities.invokeAndWait(() -> { |
| 64 | + tableLoc = table.getLocationOnScreen(); |
| 65 | + cellRect = table.getCellRect(0, 0, true); |
| 66 | + }); |
| 67 | + |
| 68 | + robot.mouseMove(tableLoc.x + cellRect.x + cellRect.width / 2, |
| 69 | + tableLoc.y + cellRect.y + cellRect.height / 2); |
| 70 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 71 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 72 | + robot.waitForIdle(); |
| 73 | + robot.delay(20); |
| 74 | + |
| 75 | + SwingUtilities.invokeAndWait(() -> { |
| 76 | + selectedRowBeforeTabPress = table.getSelectedRow(); |
| 77 | + selectedColumnBeforeTabPress = table.getSelectedColumn(); |
| 78 | + }); |
| 79 | + |
| 80 | + robot.keyPress(KeyEvent.VK_TAB); |
| 81 | + robot.keyRelease(KeyEvent.VK_TAB); |
| 82 | + robot.waitForIdle(); |
| 83 | + robot.delay(20); |
| 84 | + robot.keyPress(KeyEvent.VK_TAB); |
| 85 | + robot.keyRelease(KeyEvent.VK_TAB); |
| 86 | + robot.waitForIdle(); |
| 87 | + robot.delay(20); |
| 88 | + |
| 89 | + SwingUtilities.invokeAndWait(() -> { |
| 90 | + selectedRowAfterTabPress = table.getSelectedRow(); |
| 91 | + selectedColumnAfterTabPress = table.getSelectedColumn(); |
| 92 | + }); |
| 93 | + |
| 94 | + if (selectedRowAfterTabPress != (selectedRowBeforeTabPress + 1) |
| 95 | + && selectedColumnAfterTabPress != selectedColumnBeforeTabPress) { |
| 96 | + throw new RuntimeException("JTable's cell focus didn't shift to next" + |
| 97 | + " row first cell on TAB press"); |
| 98 | + } |
| 99 | + } finally { |
| 100 | + SwingUtilities.invokeAndWait(() -> { |
| 101 | + if (frame != null) { |
| 102 | + frame.dispose(); |
| 103 | + } |
| 104 | + }); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private static void createAndShowUI() { |
| 109 | + frame = new JFrame("Test JTable Tab Press"); |
| 110 | + table = new JTable(2, 2); |
| 111 | + frame.getContentPane().add(table); |
| 112 | + frame.setSize(200, 200); |
| 113 | + frame.setLocationRelativeTo(null); |
| 114 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 115 | + frame.setVisible(true); |
| 116 | + } |
| 117 | +} |
0 commit comments