Is there a bug with JTree?

I can't seem to get to the bottom of a problem I'm having.

I have a JTree object, for which I'm using a DefaultTreeModel. I always use TreeModel.insertNodeInto(...), and TreeModel.removeNodeFromParent(...) when inserting and deleting nodes.

But sometimes when I add/modify nodes they don't appear properly - like you can see the dotten lines towards the node, but not the string representation. So if I close the node (ie. unexpand) above the one I can't see, then the one I couldn't see reappears... but then if I re-expand the previous node, I get exceptions like:

Exception in thread"AWT-EventQueue-0" java.lang.NullPointerException

at com.sun.java.swing.plaf.windows.WindowsTreeUI.ensureRowsAreVisible(WindowsTreeUI.java:71)

at javax.swing.plaf.basic.BasicTreeUI.toggleExpandState(BasicTreeUI.java:2193)

at javax.swing.plaf.basic.BasicTreeUI.handleExpandControlClick(BasicTreeUI.java:2176)

at javax.swing.plaf.basic.BasicTreeUI.checkForClickInExpandControl(BasicTreeUI.java:2130)

at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelectionImpl(BasicTreeUI.java:3495)

at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection(BasicTreeUI.java:3480)

at javax.swing.plaf.basic.BasicTreeUI$Handler.mousePressed(BasicTreeUI.java:3461)

at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:222)

at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:221)

at java.awt.Component.processMouseEvent(Component.java:5485)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)

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)

which you can see does not contain any of my own code.

Any suggestions would be greatly appreciated.

[2976 byte] By [joel24a] at [2007-10-3 10:03:53]
# 1
can you reproduce the problem with a small sample program (few dozen lines)?
dberanskya at 2007-7-15 5:22:59 > top of Java-index,Desktop,Core GUI APIs...
# 2
may not be your issue, but i"ve seen a number of similar problems solved by wrapping "I always use TreeModel.insertNodeInto(...), and TreeModel.removeNodeFromParent(...) when inserting and deleting nodes."in a SwingUtilities.invokelater(...)
Michael_Dunna at 2007-7-15 5:22:59 > top of Java-index,Desktop,Core GUI APIs...
# 3

Well, I tried both of your suggestions. The invokerLater didn't help. But I created a small program, reproduced the error - and then found out my problem.

Here's the thing: don't use expandPath() when wanting to make sure a path is visible. You have to use scrollPathToVisible(). For whatever reason, this solved my problems.

Thanks.

joel24a at 2007-7-15 5:22:59 > top of Java-index,Desktop,Core GUI APIs...