public void jTree1_valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode selNode = (DefaultMutableTreeNode)jTree1.getLastSelectedPathComponent();
TreeNode selRoot = selNode.getParent();
selNode.removeFromParent();
}
我尝试了validate(),inValidate()都不行,有人说用updateUI(),但是这个方法显然不对,这个方法由UIManager调用,用于发出L&F已经更改的通知,并且如果手动调用将发生异常,以下是异常信息:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.plaf.basic.BasicTreeUI.isToggleEvent(BasicTreeUI.java:2192)
at javax.swing.plaf.basic.BasicTreeUI.selectPathForEvent(BasicTreeUI.java:2257)
at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection(BasicTreeUI.java:3442)
at javax.swing.plaf.basic.BasicTreeUI$Handler.mousePressed(BasicTreeUI.java:3403)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:222)
at java.awt.Component.processMouseEvent(Component.java:5485)1
at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3889)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
10 个解决方案
#1
补充:TreeNode selRoot = selNode.getParent();
这句是没有用的,我忘了删掉。
这句是没有用的,我忘了删掉。
#2
repaint()
revliadate()
revliadate()
#3
楼上说的方法没用
#4
DefaultTreeModel treeModel= new DefaultTreeModel();
DefaultMutableTreeNode selNode =(DefaultMutableTreeNode)jTree1.getLastSelectedPathComponent();
treeModel.removeNodeFromParent(selNode);
你试试这样可以不?
DefaultMutableTreeNode selNode =(DefaultMutableTreeNode)jTree1.getLastSelectedPathComponent();
treeModel.removeNodeFromParent(selNode);
你试试这样可以不?
#5
谢谢楼上回复,但我用的方法和你的一样啊
#6
我们两个的方法是不一样的,用DefaultTreeModel的removeNodeFromParent()方法删除节点,包含它的子节点,JTree会更新的。
#7
((DefaultTreeModel)jTree1.getModel()).removeNodeFromParent(selNode);
我用了这样的方法,可以删除节点,但是也产生异常:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at swingteaching.TTFrame.jTree1_valueChanged(TTFrame.java:64)
at swingteaching.TTFrame_jTree1_treeSelectionAdapter.valueChanged(TTFrame.java:77)
at javax.swing.JTree.fireValueChanged(JTree.java:2399)
at javax.swing.JTree$TreeSelectionRedirector.valueChanged(JTree.java:2770)
at javax.swing.tree.DefaultTreeSelectionModel.fireValueChanged(DefaultTreeSelectionModel.java:629)
at javax.swing.tree.DefaultTreeSelectionModel.notifyPathChange(DefaultTreeSelectionModel.java:1078)bananas
at javax.swing.tree.DefaultTreeSelectionModel.removeSelectionPaths(DefaultTreeSelectionModel.java:497)
at javax.swing.JTree.removeDescendantSelectedPaths(JTree.java:3095)
at javax.swing.JTree.removeDescendantSelectedPaths(JTree.java:3145)
at javax.swing.JTree$TreeModelHandler.treeNodesRemoved(JTree.java:3229)
at javax.swing.tree.DefaultTreeModel.fireTreeNodesRemoved(DefaultTreeModel.java:528)
at javax.swing.tree.DefaultTreeModel.nodesWereRemoved(DefaultTreeModel.java:308)
at javax.swing.tree.DefaultTreeModel.removeNodeFromParent(DefaultTreeModel.java:244)
at swingteaching.TTFrame.jTree1_valueChanged(TTFrame.java:65)
at swingteaching.TTFrame_jTree1_treeSelectionAdapter.valueChanged(TTFrame.java:77)
at javax.swing.JTree.fireValueChanged(JTree.java:2399)
at javax.swing.JTree$TreeSelectionRedirector.valueChanged(JTree.java:2770)
at javax.swing.tree.DefaultTreeSelectionModel.fireValueChanged(DefaultTreeSelectionModel.java:629)
at javax.swing.tree.DefaultTreeSelectionModel.notifyPathChange(DefaultTreeSelectionModel.java:1078)
at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPaths(DefaultTreeSelectionModel.java:287)
at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPath(DefaultTreeSelectionModel.java:170)
at javax.swing.JTree.setSelectionPath(JTree.java:1174)
at javax.swing.plaf.basic.BasicTreeUI.selectPathForEvent(BasicTreeUI.java:2256)
at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection(BasicTreeUI.java:3442)
at javax.swing.plaf.basic.BasicTreeUI$Handler.mousePressed(BasicTreeUI.java:3403)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:222)
at java.awt.Component.processMouseEvent(Component.java:5485)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3889)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
我用了这样的方法,可以删除节点,但是也产生异常:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at swingteaching.TTFrame.jTree1_valueChanged(TTFrame.java:64)
at swingteaching.TTFrame_jTree1_treeSelectionAdapter.valueChanged(TTFrame.java:77)
at javax.swing.JTree.fireValueChanged(JTree.java:2399)
at javax.swing.JTree$TreeSelectionRedirector.valueChanged(JTree.java:2770)
at javax.swing.tree.DefaultTreeSelectionModel.fireValueChanged(DefaultTreeSelectionModel.java:629)
at javax.swing.tree.DefaultTreeSelectionModel.notifyPathChange(DefaultTreeSelectionModel.java:1078)bananas
at javax.swing.tree.DefaultTreeSelectionModel.removeSelectionPaths(DefaultTreeSelectionModel.java:497)
at javax.swing.JTree.removeDescendantSelectedPaths(JTree.java:3095)
at javax.swing.JTree.removeDescendantSelectedPaths(JTree.java:3145)
at javax.swing.JTree$TreeModelHandler.treeNodesRemoved(JTree.java:3229)
at javax.swing.tree.DefaultTreeModel.fireTreeNodesRemoved(DefaultTreeModel.java:528)
at javax.swing.tree.DefaultTreeModel.nodesWereRemoved(DefaultTreeModel.java:308)
at javax.swing.tree.DefaultTreeModel.removeNodeFromParent(DefaultTreeModel.java:244)
at swingteaching.TTFrame.jTree1_valueChanged(TTFrame.java:65)
at swingteaching.TTFrame_jTree1_treeSelectionAdapter.valueChanged(TTFrame.java:77)
at javax.swing.JTree.fireValueChanged(JTree.java:2399)
at javax.swing.JTree$TreeSelectionRedirector.valueChanged(JTree.java:2770)
at javax.swing.tree.DefaultTreeSelectionModel.fireValueChanged(DefaultTreeSelectionModel.java:629)
at javax.swing.tree.DefaultTreeSelectionModel.notifyPathChange(DefaultTreeSelectionModel.java:1078)
at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPaths(DefaultTreeSelectionModel.java:287)
at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPath(DefaultTreeSelectionModel.java:170)
at javax.swing.JTree.setSelectionPath(JTree.java:1174)
at javax.swing.plaf.basic.BasicTreeUI.selectPathForEvent(BasicTreeUI.java:2256)
at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection(BasicTreeUI.java:3442)
at javax.swing.plaf.basic.BasicTreeUI$Handler.mousePressed(BasicTreeUI.java:3403)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:222)
at java.awt.Component.processMouseEvent(Component.java:5485)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3889)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
#8
另外我还想知道,如何在树上插入一个节点
#9
哦,晕,发现问题了。valuechanged触发了两次。。。。第二次触发时没有任何节点被选中。。。。
好难。。。
好难。。。
#10
顶楼主,我的问题搞定了
#1
补充:TreeNode selRoot = selNode.getParent();
这句是没有用的,我忘了删掉。
这句是没有用的,我忘了删掉。
#2
repaint()
revliadate()
revliadate()
#3
楼上说的方法没用
#4
DefaultTreeModel treeModel= new DefaultTreeModel();
DefaultMutableTreeNode selNode =(DefaultMutableTreeNode)jTree1.getLastSelectedPathComponent();
treeModel.removeNodeFromParent(selNode);
你试试这样可以不?
DefaultMutableTreeNode selNode =(DefaultMutableTreeNode)jTree1.getLastSelectedPathComponent();
treeModel.removeNodeFromParent(selNode);
你试试这样可以不?
#5
谢谢楼上回复,但我用的方法和你的一样啊
#6
我们两个的方法是不一样的,用DefaultTreeModel的removeNodeFromParent()方法删除节点,包含它的子节点,JTree会更新的。
#7
((DefaultTreeModel)jTree1.getModel()).removeNodeFromParent(selNode);
我用了这样的方法,可以删除节点,但是也产生异常:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at swingteaching.TTFrame.jTree1_valueChanged(TTFrame.java:64)
at swingteaching.TTFrame_jTree1_treeSelectionAdapter.valueChanged(TTFrame.java:77)
at javax.swing.JTree.fireValueChanged(JTree.java:2399)
at javax.swing.JTree$TreeSelectionRedirector.valueChanged(JTree.java:2770)
at javax.swing.tree.DefaultTreeSelectionModel.fireValueChanged(DefaultTreeSelectionModel.java:629)
at javax.swing.tree.DefaultTreeSelectionModel.notifyPathChange(DefaultTreeSelectionModel.java:1078)bananas
at javax.swing.tree.DefaultTreeSelectionModel.removeSelectionPaths(DefaultTreeSelectionModel.java:497)
at javax.swing.JTree.removeDescendantSelectedPaths(JTree.java:3095)
at javax.swing.JTree.removeDescendantSelectedPaths(JTree.java:3145)
at javax.swing.JTree$TreeModelHandler.treeNodesRemoved(JTree.java:3229)
at javax.swing.tree.DefaultTreeModel.fireTreeNodesRemoved(DefaultTreeModel.java:528)
at javax.swing.tree.DefaultTreeModel.nodesWereRemoved(DefaultTreeModel.java:308)
at javax.swing.tree.DefaultTreeModel.removeNodeFromParent(DefaultTreeModel.java:244)
at swingteaching.TTFrame.jTree1_valueChanged(TTFrame.java:65)
at swingteaching.TTFrame_jTree1_treeSelectionAdapter.valueChanged(TTFrame.java:77)
at javax.swing.JTree.fireValueChanged(JTree.java:2399)
at javax.swing.JTree$TreeSelectionRedirector.valueChanged(JTree.java:2770)
at javax.swing.tree.DefaultTreeSelectionModel.fireValueChanged(DefaultTreeSelectionModel.java:629)
at javax.swing.tree.DefaultTreeSelectionModel.notifyPathChange(DefaultTreeSelectionModel.java:1078)
at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPaths(DefaultTreeSelectionModel.java:287)
at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPath(DefaultTreeSelectionModel.java:170)
at javax.swing.JTree.setSelectionPath(JTree.java:1174)
at javax.swing.plaf.basic.BasicTreeUI.selectPathForEvent(BasicTreeUI.java:2256)
at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection(BasicTreeUI.java:3442)
at javax.swing.plaf.basic.BasicTreeUI$Handler.mousePressed(BasicTreeUI.java:3403)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:222)
at java.awt.Component.processMouseEvent(Component.java:5485)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3889)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
我用了这样的方法,可以删除节点,但是也产生异常:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at swingteaching.TTFrame.jTree1_valueChanged(TTFrame.java:64)
at swingteaching.TTFrame_jTree1_treeSelectionAdapter.valueChanged(TTFrame.java:77)
at javax.swing.JTree.fireValueChanged(JTree.java:2399)
at javax.swing.JTree$TreeSelectionRedirector.valueChanged(JTree.java:2770)
at javax.swing.tree.DefaultTreeSelectionModel.fireValueChanged(DefaultTreeSelectionModel.java:629)
at javax.swing.tree.DefaultTreeSelectionModel.notifyPathChange(DefaultTreeSelectionModel.java:1078)bananas
at javax.swing.tree.DefaultTreeSelectionModel.removeSelectionPaths(DefaultTreeSelectionModel.java:497)
at javax.swing.JTree.removeDescendantSelectedPaths(JTree.java:3095)
at javax.swing.JTree.removeDescendantSelectedPaths(JTree.java:3145)
at javax.swing.JTree$TreeModelHandler.treeNodesRemoved(JTree.java:3229)
at javax.swing.tree.DefaultTreeModel.fireTreeNodesRemoved(DefaultTreeModel.java:528)
at javax.swing.tree.DefaultTreeModel.nodesWereRemoved(DefaultTreeModel.java:308)
at javax.swing.tree.DefaultTreeModel.removeNodeFromParent(DefaultTreeModel.java:244)
at swingteaching.TTFrame.jTree1_valueChanged(TTFrame.java:65)
at swingteaching.TTFrame_jTree1_treeSelectionAdapter.valueChanged(TTFrame.java:77)
at javax.swing.JTree.fireValueChanged(JTree.java:2399)
at javax.swing.JTree$TreeSelectionRedirector.valueChanged(JTree.java:2770)
at javax.swing.tree.DefaultTreeSelectionModel.fireValueChanged(DefaultTreeSelectionModel.java:629)
at javax.swing.tree.DefaultTreeSelectionModel.notifyPathChange(DefaultTreeSelectionModel.java:1078)
at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPaths(DefaultTreeSelectionModel.java:287)
at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPath(DefaultTreeSelectionModel.java:170)
at javax.swing.JTree.setSelectionPath(JTree.java:1174)
at javax.swing.plaf.basic.BasicTreeUI.selectPathForEvent(BasicTreeUI.java:2256)
at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection(BasicTreeUI.java:3442)
at javax.swing.plaf.basic.BasicTreeUI$Handler.mousePressed(BasicTreeUI.java:3403)
at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:222)
at java.awt.Component.processMouseEvent(Component.java:5485)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3093)
at java.awt.Component.processEvent(Component.java:5253)
at java.awt.Container.processEvent(Container.java:1966)
at java.awt.Component.dispatchEventImpl(Component.java:3955)
at java.awt.Container.dispatchEventImpl(Container.java:2024)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3889)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)
at java.awt.Container.dispatchEventImpl(Container.java:2010)
at java.awt.Window.dispatchEventImpl(Window.java:1774)
at java.awt.Component.dispatchEvent(Component.java:3803)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)
#8
另外我还想知道,如何在树上插入一个节点
#9
哦,晕,发现问题了。valuechanged触发了两次。。。。第二次触发时没有任何节点被选中。。。。
好难。。。
好难。。。
#10
顶楼主,我的问题搞定了