Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/swing/JTable/7068740/bug7068740.java
38854 views
/*1* Copyright (c) 2013, 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/* @test24@bug 706874025@summary JTable wrapped in JLayer can't use PGUP/PGDOWN keys26@author Vladislav Karnaukhov27@run main bug706874028*/2930import javax.swing.*;31import javax.swing.plaf.LayerUI;32import javax.swing.plaf.metal.MetalLookAndFeel;33import javax.swing.table.DefaultTableModel;34import java.awt.*;35import java.awt.event.KeyEvent;36import java.lang.reflect.InvocationTargetException;37import java.util.concurrent.atomic.AtomicInteger;3839public class bug7068740 extends JFrame {4041private static Robot robot = null;42private static JTable table = null;4344bug7068740() {45super();46setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);4748DefaultTableModel model = new DefaultTableModel() {49@Override50public int getRowCount() {51return 20;52}5354@Override55public int getColumnCount() {56return 2;57}5859@Override60public Object getValueAt(int row, int column) {61return "(" + row + "," + column + ")";62}63};6465table = new JTable(model);66table.setRowSelectionInterval(0, 0);67LayerUI<JComponent> layerUI = new LayerUI<>();68JLayer<JComponent> layer = new JLayer<>(table, layerUI);69JScrollPane scrollPane = new JScrollPane(layer);70add(scrollPane);71pack();72setLocationRelativeTo(null);73}7475private static void setUp() {76try {77if (robot == null) {78robot = new Robot();79robot.setAutoDelay(50);80}8182SwingUtilities.invokeAndWait(new Runnable() {83@Override84public void run() {85bug7068740 test = new bug7068740();86test.setVisible(true);87}88});89} catch (InterruptedException e) {90e.printStackTrace();91throw new RuntimeException("Test failed");92} catch (InvocationTargetException e) {93e.printStackTrace();94throw new RuntimeException("Test failed");95} catch (AWTException e) {96e.printStackTrace();97throw new RuntimeException("Test failed");98}99}100101private static int getSelectedRow() throws Exception {102final AtomicInteger row = new AtomicInteger(-1);103SwingUtilities.invokeAndWait(new Runnable() {104@Override105public void run() {106row.set(table.getSelectedRow());107}108});109return row.intValue();110}111112private static void doTest() throws Exception {113robot.waitForIdle();114115robot.keyPress(KeyEvent.VK_PAGE_DOWN);116robot.keyRelease(KeyEvent.VK_PAGE_DOWN);117robot.waitForIdle();118119if (getSelectedRow() != 19) {120throw new RuntimeException("Test failed");121}122123robot.keyPress(KeyEvent.VK_PAGE_UP);124robot.keyRelease(KeyEvent.VK_PAGE_UP);125robot.waitForIdle();126if (getSelectedRow() != 0) {127throw new RuntimeException("Test failed");128}129}130131public static void main(String[] args) throws Exception {132try {133UIManager.setLookAndFeel(new MetalLookAndFeel());134setUp();135doTest();136} catch (UnsupportedLookAndFeelException e) {137e.printStackTrace();138throw new RuntimeException("Test failed");139}140}141}142143144