wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Treenode image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Treenode image" Watch "Treenode image" New topic
Author

Treenode image

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi all,
Can i change the image of the tree node selected?
thanks
Ian Calegory
Greenhorn

Joined: Feb 28, 2001
Posts: 3
Sure. Set the cell renderer with:
tree.setCellRenderer(treeRenderer);
And then an example treeRenderer (you can use the selected parameter to paint a different icon if the node is selected):
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.Component;
/**
* A tree renderer that displays each TreeNodeObject in a JTree with an icon
* that corresponds to its NodeType.
*

* This is an adaptation of AlbumTreeRenderer on page 354 of JFC Unleashed.
*

**/
public class TreeRenderer extends JLabel implements TreeCellRenderer {
ImageIcon iconArray[][] = new ImageIcon[2][6];
ImageIcon defaultIcon;
/**
* TreeRenderer, Constructor.
*

* The renderer must be opaque so the background
* color is painted.
*

* Icons corresponding to (int)NodeType are loaded into an Array.
* This should perhaps be improved so that an array is created with
* the exact number of elements as exists NodeTypes--so that space
* isn't wasted and so there isn't much a possibility of running out
* of NodeTypes.
**/
public TreeRenderer() {
defaultIcon = new ImageIcon( "nodetypeD.gif" );
int i;
setFont( UIManager.getFont( "Tree.font" ) );
setOpaque( true );
for( i=0; i<6; i++ ) {
if( i==1 | | i==2 ) {
iconArray[0][i] = new ImageIcon( "nodetype"+i+"a.gif" );
iconArray[1][i] = new ImageIcon( "nodetype"+i+"b.gif" );
} else {
iconArray[0][i] = new ImageIcon( "nodetype"+i+".gif") );
}
}
}
/**
* Configures the renderer based on the passed in components.
* Text for the cell is obtained from the toString() method of
* the value parameter.
*

* Foreground and background colors are obtained fom the
* UIManager.
**/
public Component getTreeCellRendererComponent( JTree tree,
Object value,
boolean selected,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus ) {
if( value != null )
setText( value.toString() );
else
setText( "" );
//set the colors for the cell.
if( selected ) {
setBackground( UIManager.getColor( "Tree.selectionBackground" ) );
setForeground( UIManager.getColor( "Tree.selectionForeground" ) );
} else {
setBackground( UIManager.getColor( "Tree.textBackground" ) );
setForeground( UIManager.getColor( "Tree.textForeground" ) );
}
setIcon( null );
if( value instanceof DefaultMutableTreeNode ) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
if( !(node.getUserObject() instanceof String) ) {
TreeNodeObject o = (TreeNodeObject)node.getUserObject();
if( o.getUID() == 0 )
setIcon( iconArray[0][0] );
else if( o.getType() != 1 && o.getType() != 2 ) {
if( o.getType() < 6 )
setIcon( iconArray[0][o.getType()] );
else
setIcon( defaultIcon );
} else {
if( expanded )
setIcon( iconArray[1][o.getType()] );
else
setIcon( iconArray[0][o.getType()] );
}
}
}
return( this );
}
}
 
jQuery in Action, 2nd edition
 
subject: Treenode image
 
Similar Threads
Loading image file
Getting rid of Java Icon on the upper left hand corner
Easy one
Unsupported Color Conversion Request
converting png to tiff and character recognition with tesseract