File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes display icon/text on right of selected node in jtree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "display icon/text on right of selected node in jtree" Watch "display icon/text on right of selected node in jtree" New topic
Author

display icon/text on right of selected node in jtree

pavan in
Ranch Hand

Joined: Oct 22, 2002
Posts: 64
I want to display an icon or a text to the right of the selected node on the JTree. Any idea on how to implement this?
Say,
100 A
where, 100 is selected and A is beside it and not selected.
Thanks
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

You can easily extend DefaultTreeCellRenderer to do this and set it as the cell renderer for your JTree by calling tree.setCellRenderer() with your new cell renderer.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
pavan in
Ranch Hand

Joined: Oct 22, 2002
Posts: 64
Nathan, I have extended defaultTreeCellRenderer. But, how to set the text so that some part of text is selected and some not selected.
say,
100 A
where 100 is selected and A is not selected.
See code below -
public Component getTreeCellRendererComponent(
JTree m_tree, Object m_value, boolean m_isSelected,
boolean m_isExpanded, boolean m_isLeaf, int m_row,
boolean m_hasFocus) {

super.getTreeCellRendererComponent( m_tree, m_value, m_isSelected,
m_isExpanded, m_isLeaf, m_row, m_hasFocus);
Component compObject = this;

if ( m_value instanceof EuamDefaultMutableTreeNode ) {
EuamDefaultMutableTreeNode node = (EuamDefaultMutableTreeNode)m_value;
setText( node.getText() );
setIcon(m_isExpanded ? node.getOpenIcon() : node.getClosedIcon() );

}
else {
setText(m_value.toString());
}
return compObject;
}
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Pavan,

Here's an example of how I added the desired functionality to the renderer... Enjoy!

pavan in
Ranch Hand

Joined: Oct 22, 2002
Posts: 64
Thanks a loot Nathan. I havent run it, but I think it will work. That was very very useful.
Thanks again.
pavan in
Ranch Hand

Joined: Oct 22, 2002
Posts: 64
Nathan,
Can I perform this task on node selection (in valuechanged method of TreeSelectionAdapter)?
I have a tree that is built initially, and I get the text (to be displayed next to node text) from the database only when the user selects the node.
Can I do this task?
Is node.setUserObject() do the task?
Thanks.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

In the example I provided, the EuamDefaultMutableTreeNode class contains the number... so you would initially start it out at 0 (or some other default value) when you created it, and then in your selection listener you would cast the selected node to a EuamDefaultMutableTreeNode and call setNumber( int ) on it...
pavan in
Ranch Hand

Joined: Oct 22, 2002
Posts: 64
Nathan,
Is it possible to associate an listener (say, mouseClick) on the text displayed on the right hand side of the node data.
i.e, for the text '100 A', can I associate an listener to the text 'A' so that when the user clicks on 'A', some method is called.
Thanks
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Yes... you could just add a MouseListener to the tree or the cell renderer.
pavan in
Ranch Hand

Joined: Oct 22, 2002
Posts: 64
I dont want the listener to be added to the text on the left hand side. MouseListener will corresponds to the whole text (left and right), isnt it?
How do I add listener to the cell renderer?
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

Sorry... just tried this out for real, and the tree "eats" all the events for the rendererers, so you can't add a listener on the renderer.

However, I did find some code here that shows how to differentiate areas of the renderer using a listener on the JTree.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: display icon/text on right of selected node in jtree