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