Path: blob/master/test/jdk/javax/swing/JTable/7068740/bug7068740.java
41154 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/*24* @test25* @key headful26* @bug 706874027* @summary JTable wrapped in JLayer can't use PGUP/PGDOWN keys28* @author Vladislav Karnaukhov29* @run main bug706874030*/3132import javax.swing.*;33import javax.swing.plaf.LayerUI;34import javax.swing.plaf.metal.MetalLookAndFeel;35import javax.swing.table.DefaultTableModel;36import java.awt.*;37import java.awt.event.KeyEvent;38import java.lang.reflect.InvocationTargetException;39import java.util.concurrent.atomic.AtomicInteger;4041public class bug7068740 extends JFrame {4243private static Robot robot = null;44private static JTable table = null;4546bug7068740() {47super();48setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);4950DefaultTableModel model = new DefaultTableModel() {51@Override52public int getRowCount() {53return 20;54}5556@Override57public int getColumnCount() {58return 2;59}6061@Override62public Object getValueAt(int row, int column) {63return "(" + row + "," + column + ")";64}65};6667table = new JTable(model);68table.setRowSelectionInterval(0, 0);69LayerUI<JComponent> layerUI = new LayerUI<>();70JLayer<JComponent> layer = new JLayer<>(table, layerUI);71JScrollPane scrollPane = new JScrollPane(layer);72add(scrollPane);73pack();74setLocationRelativeTo(null);75}7677private static void setUp() {78try {79if (robot == null) {80robot = new Robot();81robot.setAutoDelay(50);82}8384SwingUtilities.invokeAndWait(new Runnable() {85@Override86public void run() {87bug7068740 test = new bug7068740();88test.setVisible(true);89}90});91} catch (InterruptedException e) {92e.printStackTrace();93throw new RuntimeException("Test failed");94} catch (InvocationTargetException e) {95e.printStackTrace();96throw new RuntimeException("Test failed");97} catch (AWTException e) {98e.printStackTrace();99throw new RuntimeException("Test failed");100}101}102103private static int getSelectedRow() throws Exception {104final AtomicInteger row = new AtomicInteger(-1);105SwingUtilities.invokeAndWait(new Runnable() {106@Override107public void run() {108row.set(table.getSelectedRow());109}110});111return row.intValue();112}113114private static void doTest() throws Exception {115robot.waitForIdle();116117robot.keyPress(KeyEvent.VK_PAGE_DOWN);118robot.keyRelease(KeyEvent.VK_PAGE_DOWN);119robot.waitForIdle();120121if (getSelectedRow() != 19) {122throw new RuntimeException("Test failed");123}124125robot.keyPress(KeyEvent.VK_PAGE_UP);126robot.keyRelease(KeyEvent.VK_PAGE_UP);127robot.waitForIdle();128if (getSelectedRow() != 0) {129throw new RuntimeException("Test failed");130}131}132133public static void main(String[] args) throws Exception {134try {135UIManager.setLookAndFeel(new MetalLookAndFeel());136setUp();137doTest();138} catch (UnsupportedLookAndFeelException e) {139e.printStackTrace();140throw new RuntimeException("Test failed");141}142}143}144145146