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 /* 25 * @test 26 * @key headful 27 * @bug 8003400 28 * @summary Tests that JTree shows the last row 29 * @author Sergey Malenkov 30 * @run main/othervm Test8003400 31 * @run main/othervm Test8003400 reverse 32 * @run main/othervm Test8003400 system 33 * @run main/othervm Test8003400 system reverse 34 */ 35 36 import sun.awt.SunToolkit; 37 38 import java.awt.Rectangle; 39 import java.awt.Robot; 40 import java.awt.Toolkit; 41 import java.awt.event.KeyEvent; 42 import java.util.Arrays; 43 import java.util.Collections; 44 import java.util.List; 45 import javax.swing.JFrame; 46 import javax.swing.JScrollPane; 47 import javax.swing.JTree; 48 import javax.swing.SwingUtilities; 49 import javax.swing.UIManager; 50 import javax.swing.tree.DefaultMutableTreeNode; 51 52 public class Test8003400 { 53 54 private static final String TITLE = "Test JTree with a large model"; 55 private static List<String> OBJECTS = Arrays.asList(TITLE, "x", "y", "z"); 56 private static JScrollPane pane; 57 58 public static void main(String[] args) throws Exception { 59 for (String arg : args) { 60 if (arg.equals("reverse")) { 61 Collections.reverse(OBJECTS); 62 } 63 if (arg.equals("system")) { 64 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 65 } 66 } 67 SwingUtilities.invokeAndWait(new Runnable() { 68 public void run() { 69 DefaultMutableTreeNode root = new DefaultMutableTreeNode(); 70 71 JTree tree = new JTree(root); 72 tree.setLargeModel(true); 73 tree.setRowHeight(16); 74 75 JFrame frame = new JFrame(TITLE); 76 frame.add(pane = new JScrollPane(tree)); 77 frame.setSize(200, 100); 78 frame.setLocationRelativeTo(null); 79 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 80 frame.setVisible(true); 81 82 for (String object : OBJECTS) { 83 root.add(new DefaultMutableTreeNode(object)); 84 } 85 tree.expandRow(0); 86 } 87 }); 88 89 SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 90 toolkit.realSync(500); 91 new Robot().keyPress(KeyEvent.VK_END); 92 toolkit.realSync(500); 93 94 SwingUtilities.invokeAndWait(new Runnable() { 95 public void run() { 96 JTree tree = (JTree) pane.getViewport().getView(); 97 Rectangle inner = tree.getRowBounds(tree.getRowCount() - 1); 98 Rectangle outer = SwingUtilities.convertRectangle(tree, inner, pane); 99 outer.y += tree.getRowHeight() - 1 - pane.getVerticalScrollBar().getHeight(); 100 // error reporting only for automatic testing 101 if (null != System.getProperty("test.src", null)) { 102 SwingUtilities.getWindowAncestor(pane).dispose(); 103 if (outer.y != 0) { 104 throw new Error("TEST FAILED: " + outer.y); 105 } 106 } 107 } 108 }); 109 } 110 }