11/*
2- * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2626 * @key headful
2727 * @bug 7068740
2828 * @summary JTable wrapped in JLayer can't use PGUP/PGDOWN keys
29- * @author Vladislav Karnaukhov
3029 * @run main bug7068740
3130 */
3231
33- import javax .swing .*;
32+ import java .awt .Robot ;
33+ import java .awt .event .KeyEvent ;
34+ import javax .swing .JComponent ;
35+ import javax .swing .JFrame ;
36+ import javax .swing .JLayer ;
37+ import javax .swing .JScrollPane ;
38+ import javax .swing .JTable ;
3439import javax .swing .plaf .LayerUI ;
3540import javax .swing .plaf .metal .MetalLookAndFeel ;
41+ import javax .swing .SwingUtilities ;
42+ import javax .swing .UIManager ;
43+ import javax .swing .UnsupportedLookAndFeelException ;
3644import javax .swing .table .DefaultTableModel ;
37- import java .awt .*;
38- import java .awt .event .KeyEvent ;
39- import java .lang .reflect .InvocationTargetException ;
4045import java .util .concurrent .atomic .AtomicInteger ;
4146
42- public class bug7068740 extends JFrame {
47+ public class bug7068740 {
4348
4449 private static Robot robot = null ;
4550 private static JTable table = null ;
51+ private static JFrame frame ;
4652
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- }
53+ private static void setUp () throws Exception {
54+ if (robot == null ) {
55+ robot = new Robot ();
56+ robot .setAutoDelay (100 );
57+ }
6158
59+ SwingUtilities .invokeAndWait (new Runnable () {
6260 @ Override
63- public Object getValueAt (int row , int column ) {
64- return "(" + row + "," + column + ")" ;
65- }
66- };
67-
68- table = new JTable (model );
69- table .setRowSelectionInterval (0 , 0 );
70- LayerUI <JComponent > layerUI = new LayerUI <>();
71- JLayer <JComponent > layer = new JLayer <>(table , layerUI );
72- JScrollPane scrollPane = new JScrollPane (layer );
73- add (scrollPane );
74- pack ();
75- setLocationRelativeTo (null );
76- }
77-
78- private static void setUp () {
79- try {
80- if (robot == null ) {
81- robot = new Robot ();
82- robot .setAutoDelay (50 );
61+ public void run () {
62+ DefaultTableModel model = new DefaultTableModel () {
63+ @ Override
64+ public int getRowCount () {
65+ return 20 ;
66+ }
67+
68+ @ Override
69+ public int getColumnCount () {
70+ return 2 ;
71+ }
72+
73+ @ Override
74+ public Object getValueAt (int row , int column ) {
75+ return "(" + row + "," + column + ")" ;
76+ }
77+ };
78+
79+ table = new JTable (model );
80+ table .setRowSelectionInterval (0 , 0 );
81+ LayerUI <JComponent > layerUI = new LayerUI <>();
82+ JLayer <JComponent > layer = new JLayer <>(table , layerUI );
83+ JScrollPane scrollPane = new JScrollPane (layer );
84+ frame = new JFrame ();
85+ frame .add (scrollPane );
86+ frame .pack ();
87+ frame .setLocationRelativeTo (null );
88+ frame .setVisible (true );
8389 }
84-
85- SwingUtilities .invokeAndWait (new Runnable () {
86- @ Override
87- public void run () {
88- bug7068740 test = new bug7068740 ();
89- test .setVisible (true );
90- }
91- });
92- } catch (InterruptedException e ) {
93- e .printStackTrace ();
94- throw new RuntimeException ("Test failed" );
95- } catch (InvocationTargetException e ) {
96- e .printStackTrace ();
97- throw new RuntimeException ("Test failed" );
98- } catch (AWTException e ) {
99- e .printStackTrace ();
100- throw new RuntimeException ("Test failed" );
101- }
90+ });
10291 }
10392
10493 private static int getSelectedRow () throws Exception {
@@ -123,6 +112,7 @@ private static void doTest() throws Exception {
123112 throw new RuntimeException ("Test failed" );
124113 }
125114
115+ robot .delay (1000 );
126116 robot .keyPress (KeyEvent .VK_PAGE_UP );
127117 robot .keyRelease (KeyEvent .VK_PAGE_UP );
128118 robot .waitForIdle ();
@@ -135,10 +125,18 @@ public static void main(String[] args) throws Exception {
135125 try {
136126 UIManager .setLookAndFeel (new MetalLookAndFeel ());
137127 setUp ();
128+ robot .waitForIdle ();
129+ robot .delay (1000 );
138130 doTest ();
139131 } catch (UnsupportedLookAndFeelException e ) {
140132 e .printStackTrace ();
141133 throw new RuntimeException ("Test failed" );
134+ } finally {
135+ SwingUtilities .invokeAndWait (() -> {
136+ if (frame != null ) {
137+ frame .dispose ();
138+ }
139+ });
142140 }
143141 }
144142}
0 commit comments