1 /* 2 * Copyright (c) 2013, 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 /* @test 25 @bug 7068740 26 @summary JTable wrapped in JLayer can't use PGUP/PGDOWN keys 27 @author Vladislav Karnaukhov 28 @run main bug7068740 29 */ 30 31 import sun.awt.SunToolkit; 32 33 import javax.swing.*; 34 import javax.swing.plaf.LayerUI; 35 import javax.swing.plaf.metal.MetalLookAndFeel; 36 import javax.swing.table.DefaultTableModel; 37 import java.awt.*; 38 import java.awt.event.KeyEvent; 39 import java.lang.reflect.InvocationTargetException; 40 import java.util.concurrent.atomic.AtomicInteger; 41 42 public class bug7068740 extends JFrame { 43 44 private static Robot robot = null; 45 private static JTable table = null; 46 private static SunToolkit toolkit = null; 47 48 bug7068740() { 49 super(); 50 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 51 52 DefaultTableModel model = new DefaultTableModel() { 53 @Override 54 public int getRowCount() { 55 return 20; 56 } 57 58 @Override 59 public int getColumnCount() { 60 return 2; 61 } 62 63 @Override 64 public Object getValueAt(int row, int column) { 65 return "(" + row + "," + column + ")"; 66 } 67 }; 68 69 table = new JTable(model); 70 table.setRowSelectionInterval(0, 0); 71 LayerUI<JComponent> layerUI = new LayerUI<>(); 72 JLayer<JComponent> layer = new JLayer<>(table, layerUI); 73 JScrollPane scrollPane = new JScrollPane(layer); 74 add(scrollPane); 75 pack(); 76 setLocationRelativeTo(null); 77 } 78 79 private static void setUp() { 80 try { 81 if (robot == null) { 82 robot = new Robot(); 83 robot.setAutoDelay(50); 84 } 85 86 if (toolkit == null) { 87 toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 88 } 89 90 SwingUtilities.invokeAndWait(new Runnable() { 91 @Override 92 public void run() { 93 bug7068740 test = new bug7068740(); 94 test.setVisible(true); 95 } 96 }); 97 } catch (InterruptedException e) { 98 e.printStackTrace(); 99 throw new RuntimeException("Test failed"); 100 } catch (InvocationTargetException e) { 101 e.printStackTrace(); 102 throw new RuntimeException("Test failed"); 103 } catch (AWTException e) { 104 e.printStackTrace(); 105 throw new RuntimeException("Test failed"); 106 } 107 } 108 109 private static int getSelectedRow() throws Exception { 110 final AtomicInteger row = new AtomicInteger(-1); 111 SwingUtilities.invokeAndWait(new Runnable() { 112 @Override 113 public void run() { 114 row.set(table.getSelectedRow()); 115 } 116 }); 117 return row.intValue(); 118 } 119 120 private static void doTest() throws Exception { 121 toolkit.realSync(); 122 123 robot.keyPress(KeyEvent.VK_PAGE_DOWN); 124 robot.keyRelease(KeyEvent.VK_PAGE_DOWN); 125 toolkit.realSync(); 126 127 if (getSelectedRow() != 19) { 128 throw new RuntimeException("Test failed"); 129 } 130 131 robot.keyPress(KeyEvent.VK_PAGE_UP); 132 robot.keyRelease(KeyEvent.VK_PAGE_UP); 133 toolkit.realSync(); 134 if (getSelectedRow() != 0) { 135 throw new RuntimeException("Test failed"); 136 } 137 } 138 139 public static void main(String[] args) throws Exception { 140 try { 141 UIManager.setLookAndFeel(new MetalLookAndFeel()); 142 setUp(); 143 doTest(); 144 } catch (UnsupportedLookAndFeelException e) { 145 e.printStackTrace(); 146 throw new RuntimeException("Test failed"); 147 } 148 } 149 }