Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to change JTree's node icon manually?

843807Jul 27 2010 — edited Jul 27 2010
Hi, I'm currently making messenger application using awt and swing. I've searched in google and here(got this), but till now I haven't got solution.

I want to change user's status icon in the buddy list if the user's status is changed(such like offline and online).

But for now I want to try to change the icon directly by pressing a button first before thinking about listener for the status.

I have this tree
	CustomCellRenderer rosterTreeRenderer = new CustomCellRenderer();
        DefaultMutableTreeNode rosterRoot = new DefaultMutableTreeNode("root");
        DefaultMutableTreeNode rosterGroup = null;

	    rosterGroup = new DefaultMutableTreeNode("group 1");
	    rosterRoot.add(rosterGroup);
	    rosterGroup.add(new DefaultMutableTreeNode("abc"));
	    rosterGroup.add(new DefaultMutableTreeNode("dce"));
	    
	    rosterGroup = new DefaultMutableTreeNode("group 2");
	    rosterRoot.add(rosterGroup);
	    rosterGroup.add(new DefaultMutableTreeNode("zzz"));
	    rosterGroup.add(new DefaultMutableTreeNode("xxx"));
	    rosterGroup.add(new DefaultMutableTreeNode("yyy"));

            rosterTree.setCellRenderer(rosterTreeRenderer);
This is the cell renderer
public class CustomCellRenderer extends DefaultTreeCellRenderer
{
	private static final long serialVersionUID = 1L;

	public CustomCellRenderer(){

	}

        public Component getTreeCellRendererComponent( JTree tree, Object value,
		      boolean isSelected, boolean expanded, boolean leaf, int row,
		      boolean hasFocus )
	{
		DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
		String	labelText = (String)node.getUserObject();

		if (isSelected)
		{
			System.out.println("selected");
			this.setIcon(UIManager.getIcon("Tree.openIcon"));
			//((DefaultTreeModel) tree.getModel()).nodeChanged(node);
		}
			
		
        else if (leaf) {
            this.setIcon(UIManager.getIcon("Tree.leafIcon"));
          } else if (expanded) {
        	  this.setIcon(null);
          } else {
        	  this.setIcon(null);
          }

		setText(labelText);
		return this;
	}
	
    public Color getBackgroundNonSelectionColor() {
        return(null);
    }

    public Color getBackground() {
        return(null);
    }
}
Whenever I click the user, the user's icon change, the first phase of my testing is working.

But after that I'm confused, how can I change the icon when I click a button at the roster frame. How to invoke and send parameter to change icon of specified user? I already read DefaultTreeCellRenderer javadoc but still haven't found clue to what I wanted, most of the methods noted with "Overridden for performance reasons".

I've already searched for several days for this problem.. Hope someone can help me..

Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 24 2010
Added on Jul 27 2010
5 comments
406 views