1 /* 2 * Copyright (c) 2012, 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 6505523 28 * @summary NullPointerException in BasicTreeUI when a node is removed by expansion listener 29 * @author Alexandr Scherbatiy 30 * @run main bug6505523 31 */ 32 import java.awt.Point; 33 import java.awt.Rectangle; 34 import java.awt.Robot; 35 import java.awt.Toolkit; 36 import java.awt.event.InputEvent; 37 import javax.swing.JFrame; 38 import javax.swing.JScrollPane; 39 import javax.swing.JTree; 40 import javax.swing.SwingUtilities; 41 import javax.swing.event.TreeExpansionEvent; 42 import javax.swing.event.TreeExpansionListener; 43 import javax.swing.tree.DefaultMutableTreeNode; 44 import javax.swing.tree.DefaultTreeModel; 45 import javax.swing.tree.TreeNode; 46 import sun.awt.SunToolkit; 47 48 public class bug6505523 { 49 50 private static JTree tree; 51 52 public static void main(String[] args) throws Exception { 53 SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit(); 54 Robot robot = new Robot(); 55 robot.setAutoDelay(50); 56 57 SwingUtilities.invokeAndWait(new Runnable() { 58 59 @Override 60 public void run() { 61 createAndShowGUI(); 62 } 63 }); 64 65 toolkit.realSync(); 66 67 Point point = getRowPointToClick(2); 68 robot.mouseMove(point.x, point.y); 69 robot.mousePress(InputEvent.BUTTON1_MASK); 70 robot.mouseRelease(InputEvent.BUTTON1_MASK); 71 72 toolkit.realSync(); 73 74 } 75 76 private static Point getRowPointToClick(final int row) throws Exception { 77 78 final Point[] result = new Point[1]; 79 80 SwingUtilities.invokeAndWait(new Runnable() { 81 82 @Override 83 public void run() { 84 Rectangle rect = tree.getRowBounds(row); 85 Point point = new Point(rect.x - 5, rect.y + rect.height / 2); 86 SwingUtilities.convertPointToScreen(point, tree); 87 result[0] = point; 88 } 89 }); 90 91 return result[0]; 92 } 93 94 private static void createAndShowGUI() { 95 final DefaultMutableTreeNode root = new DefaultMutableTreeNode("Problem with NPE under JDK 1.6"); 96 final DefaultMutableTreeNode problematic = new DefaultMutableTreeNode("Expand me and behold a NPE in stderr"); 97 problematic.add(new DefaultMutableTreeNode("some content")); 98 root.add(new DefaultMutableTreeNode("irrelevant...")); 99 root.add(problematic); 100 101 final DefaultTreeModel model = new DefaultTreeModel(root); 102 tree = new JTree(model); 103 tree.setRootVisible(true); 104 tree.setShowsRootHandles(true); 105 tree.expandRow(0); 106 tree.collapseRow(2); 107 108 // this is critical - without dragEnabled everything works 109 tree.setDragEnabled(true); 110 111 tree.addTreeExpansionListener(new TreeExpansionListener() { 112 113 @Override 114 public void treeExpanded(TreeExpansionEvent event) { 115 TreeNode parent = problematic.getParent(); 116 if (parent instanceof DefaultMutableTreeNode) { 117 model.removeNodeFromParent(problematic); 118 } 119 } 120 121 @Override 122 public void treeCollapsed(TreeExpansionEvent event) { 123 } 124 }); 125 126 JFrame frame = new JFrame("JTree Problem"); 127 frame.add(new JScrollPane(tree)); 128 frame.setSize(500, 300); 129 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 130 frame.setLocationRelativeTo(null); 131 frame.setVisible(true); 132 } 133 }