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
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.
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) {
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.
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...
-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
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
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?