[SWING] Problem with JTree

M

Malte Schneider

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.

Thx, Malte
 
R

Roedy Green

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.

Most problems I have had with JTree turned out to be thread problems.

JTree is single thread and must be invoked on the EDT thread including
the fireChange.... methods.

Yet your TreeModel will likely be invoked by your app from some other
thread.

I have collected bits of JTree lore than may be useful.

See http://mindprod.com/jgloss/jtree.html
 
D

Daniele Futtorovic

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top