Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How to change JTree's node icon manually?

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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, 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


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    489
    Thanks
    8
    Thanked 45 Times in 43 Posts

    Default Re: How to change JTree's node icon manually?


  3. #3
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    489
    Thanks
    8
    Thanked 45 Times in 43 Posts

    Default Re: How to change JTree's node icon manually?

    Another cross post
    Swing - How to change JTree's node icon manually?

    db

Similar Threads

  1. setting cookies manually
    By dotanguy in forum Java Networking
    Replies: 2
    Last Post: July 2nd, 2010, 09:51 AM
  2. TreeNode vs. Node
    By Kumarrrr in forum Java Theory & Questions
    Replies: 1
    Last Post: March 27th, 2010, 06:06 AM
  3. [SOLVED] I cant load the icon!!!!
    By chronoz13 in forum AWT / Java Swing
    Replies: 12
    Last Post: January 22nd, 2010, 02:52 AM
  4. Xml-Node Retrieval
    By prasb in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: December 4th, 2009, 11:44 AM
  5. Icon change and lib folder problem
    By LeonLanford in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 21st, 2009, 03:00 AM