Re: [SWING] Problem with JTree



On 2008-02-11 20:59 +0100, Malte Schneider allegedly wrote:
Hello there,

I've got a problem using JTree.
I use a subclass of JTree that overwrites getCellRenderer() to get full control over the cell renders. Furthermore i use my own TreeModel and TreeNode implementations. These classes read method names and return values of any object.

The whole thing works fine for most object except for one object/class. If i try to display the structure and content of the object using my tree the tree is not displayed correctly. First nothing of the tree is displayed. But if i click onto it trying to select the hidden nodes some parts of the trees are displayed but never the whole tree.

My TreeCellRenderer implementations are subclasses of DefaultTreeCellRenderer and overwrite getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) to determine the current value and call the super method.

Additionally there's a TreeSelectionListener registered at the JTree that triggers the display of extra information on a separate JPanel.

While selecting node the following exception is written onto the console without killing the application:

java.lang.NullPointerException
at javax.swing.plaf.basic.BasicTreeUI$MouseHandler.handleSelection(BasicTreeUI.java:2847)

at javax.swing.plaf.basic.BasicTreeUI$MouseHandler.mousePressed(BasicTreeUI.java:2824)

at java.awt.AWTEventMulticaster.mousePressed(AWTEventMulticaster.java:222)
at java.awt.Component.processMouseEvent(Component.java:5097)
at java.awt.Component.processEvent(Component.java:4897)
at java.awt.Container.processEvent(Container.java:1569)
at java.awt.Component.dispatchEventImpl(Component.java:3615)
at java.awt.Container.dispatchEventImpl(Container.java:1627)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:3483)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3195)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3128)
at java.awt.Container.dispatchEventImpl(Container.java:1613)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3477)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:480)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:201)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:145)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:137)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)


I tried to find answers using Google but every hit talks about a TreeExpansionListener calling updateUI though i don't use this in my code.

I hope anyone knows how to fix the problem.


Read the source code! It's part of the JDK (src.zip; normally accessible
through your IDE). Open the source for BasicTreeUI, go to line 2847,
look what it's trying to do. This should get you started.

I would have loved to learn the answer myself, unfortunately, the code I
see on my machine in BasicTreeUI:2847 cannot possibly throw an NPE. I
conclude that you're not using the same version as I am (1.6.0_04).

Which version are you using?

DF.
.



Relevant Pages

  • [SWING] Problem with JTree
    ... I use a subclass of JTree that overwrites getCellRendererto get full control over the cell renders. ... If i try to display the structure and content of the object using my tree the tree is not displayed correctly. ... My TreeCellRenderer implementations are subclasses of DefaultTreeCellRenderer and overwrite getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) to determine the current value and call the super method. ... Additionally there's a TreeSelectionListener registered at the JTree that triggers the display of extra information on a separate JPanel. ...
    (comp.lang.java.programmer)
  • Display xml file in JTree: Select information to be shown in node label
    ... I have managed to write a small java program that display an xml file ... This was achieved by defining a suitable tree model. ... and the corresponding branches in the JTree can be opened ...
    (comp.lang.java)
  • Re: [SWING] Problem with JTree
    ... If i try to display the structure and content of the object using my ... tree the tree is not displayed correctly. ... Most problems I have had with JTree turned out to be thread problems. ...
    (comp.lang.java.programmer)
  • Re: TreeCellRenderer.setIcon(null) -> performance loss
    ... I would guess the JTree is very ... > refresh the tree layout). ... don't want to display them? ...
    (comp.lang.java.gui)
  • Re: Dynamically Update JTree Icons
    ... icon change when the node is opened. ... When I create my tree, ... JTree tree = new JTree; ... Object value, boolean selected, boolean expanded, ...
    (comp.lang.java.gui)