jdk/test/javax/swing/JTable/7068740/bug7068740.java
changeset 17897 fb367e67916d
child 20143 c8d62571d319
equal deleted inserted replaced
17896:271991446515 17897:fb367e67916d
       
     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 
       
    41 public class bug7068740 extends JFrame {
       
    42 
       
    43     private static Robot robot = null;
       
    44     private static JTable table = null;
       
    45     private static SunToolkit toolkit = null;
       
    46 
       
    47     bug7068740() {
       
    48         super();
       
    49         setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
       
    50 
       
    51         DefaultTableModel model = new DefaultTableModel() {
       
    52             @Override
       
    53             public int getRowCount() {
       
    54                 return 20;
       
    55             }
       
    56 
       
    57             @Override
       
    58             public int getColumnCount() {
       
    59                 return 2;
       
    60             }
       
    61 
       
    62             @Override
       
    63             public Object getValueAt(int row, int column) {
       
    64                 return "(" + row + "," + column + ")";
       
    65             }
       
    66         };
       
    67 
       
    68         table = new JTable(model);
       
    69         LayerUI<JComponent> layerUI = new LayerUI<>();
       
    70         JLayer<JComponent> layer = new JLayer<>(table, layerUI);
       
    71         JScrollPane scrollPane = new JScrollPane(layer);
       
    72         add(scrollPane);
       
    73         pack();
       
    74         setLocationRelativeTo(null);
       
    75     }
       
    76 
       
    77     private static void setUp() {
       
    78         try {
       
    79             if (robot == null) {
       
    80                 robot = new Robot();
       
    81                 robot.setAutoDelay(20);
       
    82             }
       
    83 
       
    84             if (toolkit == null) {
       
    85                 toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
       
    86             }
       
    87 
       
    88             SwingUtilities.invokeAndWait(new Runnable() {
       
    89                 @Override
       
    90                 public void run() {
       
    91                     bug7068740 test = new bug7068740();
       
    92                     test.setVisible(true);
       
    93                 }
       
    94             });
       
    95         } catch (InterruptedException e) {
       
    96             e.printStackTrace();
       
    97             throw new RuntimeException("Test failed");
       
    98         } catch (InvocationTargetException e) {
       
    99             e.printStackTrace();
       
   100             throw new RuntimeException("Test failed");
       
   101         } catch (AWTException e) {
       
   102             e.printStackTrace();
       
   103             throw new RuntimeException("Test failed");
       
   104         }
       
   105     }
       
   106 
       
   107     private static void doTest() {
       
   108         toolkit.realSync();
       
   109         table.setRowSelectionInterval(0, 0);
       
   110 
       
   111         robot.keyPress(KeyEvent.VK_PAGE_DOWN);
       
   112         toolkit.realSync();
       
   113         if (table.getSelectedRow() != 19) {
       
   114             throw new RuntimeException("Test failed");
       
   115         }
       
   116 
       
   117         robot.keyPress(KeyEvent.VK_PAGE_UP);
       
   118         toolkit.realSync();
       
   119         if (table.getSelectedRow() != 0) {
       
   120             throw new RuntimeException("Test failed");
       
   121         }
       
   122     }
       
   123 
       
   124     public static void main(String[] args) {
       
   125         try {
       
   126             UIManager.setLookAndFeel(new MetalLookAndFeel());
       
   127             setUp();
       
   128             doTest();
       
   129         } catch (UnsupportedLookAndFeelException e) {
       
   130             e.printStackTrace();
       
   131             throw new RuntimeException("Test failed");
       
   132         }
       
   133     }
       
   134 }