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