Results 1 to 4 of 4
  1. #1
    LeonLanford is offline Member
    Join Date
    Oct 2009
    Posts
    29
    Rep Power
    0

    Default How to change JTree's node icon manually?

    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

    Java Code:
    	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

    Java Code:
    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

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Can´t change selected/pressed Icon of a JMenu
    By Valhallon in forum AWT / Swing
    Replies: 3
    Last Post: 07-08-2011, 10:16 PM
  2. Replies: 1
    Last Post: 07-10-2010, 04:21 AM
  3. How to change Window Icon
    By sharafat in forum AWT / Swing
    Replies: 7
    Last Post: 01-15-2010, 12:37 AM
  4. Icon change and lib folder problem
    By LeonLanford in forum New To Java
    Replies: 10
    Last Post: 10-21-2009, 04:13 PM
  5. Handle exception manually
    By John_28 in forum New To Java
    Replies: 2
    Last Post: 06-05-2008, 11:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •