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