当前位置: 代码迷 >> J2SE >> 这么简单的有关问题难道没人知道吗?从JTree删除或增加一个节点后,怎么更新显示
  详细解决方案

这么简单的有关问题难道没人知道吗?从JTree删除或增加一个节点后,怎么更新显示

热度:338   发布时间:2016-04-24 17:23:28.0
这么简单的问题难道没人知道吗?从JTree删除或增加一个节点后,如何更新显示?
以下是我删除节点的代码:

        public   void   jTree1_valueChanged(TreeSelectionEvent   e)   {
                DefaultMutableTreeNode   selNode   =   (DefaultMutableTreeNode)jTree1.getLastSelectedPathComponent();
                TreeNode   selRoot   =   selNode.getParent();
                selNode.removeFromParent();
        }

我尝试了validate(),inValidate()都不行,有人说用revalidate,repaint,也不行,也有人说用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)

------解决方案--------------------
DefaultTreeModel treeModel= new DefaultTreeModel();
DefaultMutableTreeNode selNode =(DefaultMutableTreeNode)jTree1.getLastSelectedPathComponent();
treeModel.removeNodeFromParent(selNode);

你试试这样可以不?