Hi,
I am working on a user tree which is similar to that of an instant messenger. I got a problem regarding the setting of the status of a user. When one user changed it status (for example from ONLINE to BUSY), there is no change in icon to reflect the change in status in other user's user tree.

What's puzzling is that the setToolTipText() does reflect the change in status. I tried to do a updateUI() as in the case when a user is BUSY, but it still does not work. Hope somebody can help me with this.

A thing to note is that the icons do work, because when a user is added, it has the online icon. if i use the other icons as the default icon when a new user is just added to the user tree, they can be seen also, meaning that all the .gif files are accessible.

My code as given below(modified from the java tutorial),please advise.

Thanks in advance

Regards

Code:
package myprojects.fyp;
 
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
import java.awt.*;
 
import java.util.*;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
 
 
public class TreeExpandEventDemo2 extends JPanel {
	
    DemoArea demoArea;
    JTextArea textArea;
    final static String newline = "\n";
    MainFrame frame;
    
 
    public TreeExpandEventDemo2(MainFrame frame, User user,Vector vector) {
    	
       	
        super(new GridBagLayout());
        
        GridBagLayout gridbag = (GridBagLayout)getLayout();
        GridBagConstraints c = new GridBagConstraints();
 
        c.fill = GridBagConstraints.BOTH;
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.weightx = 1.0;
        c.weighty = 1.0;
 
        c.insets = new Insets(1, 1, 1, 1);
        
        this.frame = frame;
        
        demoArea = new DemoArea(frame, user, vector);
        
        gridbag.setConstraints(demoArea, c);
        add(demoArea);
 
        c.insets = new Insets(0, 0, 0, 0);
 
        setPreferredSize(new Dimension(220, 350));//size of frame with user tree
        setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
        this.frame = frame; 
    }
    
    public void updateUser(User user)
	{
		demoArea.updateUser(user);
	}
	
	public void removeUser(User user)
	{
		demoArea.removeUser(user);
	}
    
    class DemoArea extends JScrollPane
                   implements TreeExpansionListener,
                              TreeWillExpandListener, ChatConstants {
        Dimension minSize = new Dimension(100, 60);
        
        JTree tree;
        private Vector _userlist ;
        private Hashtable nodeTable = new Hashtable();
        MainFrame frame;
        User user;
        private JTree usrtree;
        private JScrollPane scrollPane;
 
     
        private DefaultTreeModel treeModel;
    	TreeNode rootNode1;
    	private DefaultMutableTreeNode rootNode;
    	
        public DemoArea(MainFrame frame, User user, Vector vector) {
         
            _userlist = vector;
            this.user = user;
            this.frame = frame;
            
            //Create the nodes.
            rootNode = new DefaultMutableTreeNode("Friends");
            rootNode1 = createNodes();
           	
           	//Create a tree that allows one selection at a time.
            tree = new JTree(rootNode);
            tree.addTreeExpansionListener(this);
            tree.addTreeWillExpandListener(this);
            tree.setCellRenderer(new MyRenderer());
			tree.getSelectionModel().setSelectionMode
						(TreeSelectionModel.SINGLE_TREE_SELECTION);
			
			//Enable tool tips.
			ToolTipManager.sharedInstance().registerComponent(tree);
            treeModel = new DefaultTreeModel(rootNode);
         
            setViewportView(tree);
            
 
            treeModel = new DefaultTreeModel(rootNode);
	
          	tree.addMouseListener(
          		new MouseListener(){
          			public void mousePressed(MouseEvent e) {
       				}	
 
   					public void mouseReleased(MouseEvent e) {
    				}
 
    				public void mouseEntered(MouseEvent e) {
    				}
    				public void mouseExited(MouseEvent e) {
    				}
 
    				public void mouseClicked(MouseEvent e) {
            		
                    	int selRow = tree.getRowForLocation(e.getX(), e.getY());
						TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
						DefaultMutableTreeNode node;
						if(selRow > 0 ) {
							if(e.getClickCount() == 2) {
							node = (DefaultMutableTreeNode)selPath.getLastPathComponent();
							
							if (node.isLeaf()) {
								User user = (User)(node.getUserObject());
								
								if(user.isOnline != OFFLINE) {
					createFrame(user);//original code not commented
				} else {
					JOptionPane.showMessageDialog(null,
							"Click on an online user to send a message",
							"Error",JOptionPane.INFORMATION_MESSAGE);
				}
								}
							else
								System.out.println("not leaf");
							}	
						
						}	
    				}
          		}
          	);
 
          	tree.updateUI() ;
        }
    
        private TreeNode createNodes() {
            DefaultMutableTreeNode root;
            DefaultMutableTreeNode grandparent;
            DefaultMutableTreeNode parent;
            DefaultMutableTreeNode child;
			
			User myself = user;
            root = rootNode; 
 
            DefaultMutableTreeNode user1 = null;
 
            for(int count = 0; count<_userlist.size();count++) {
				user1 = new DefaultMutableTreeNode((User)_userlist.elementAt(count));
				if (!(user1.toString()).equals(user.toString())){
					nodeTable.put(((User)_userlist.elementAt(count)).toString(),user1);
        			root.add(user1);
        		}	
            }
            return root;
        }
        
        public void addUser(Object child)
	{
		DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child);
 
		treeModel.insertNodeInto(childNode,rootNode,rootNode.getChildCount());
		
		nodeTable.put(((User)child).toString(),childNode);
		tree.updateUI() ;
		
	}
 
	public void removeUser(User user)
	{
 
		MutableTreeNode node = (MutableTreeNode)nodeTable.get(user.toString());
		treeModel.removeNodeFromParent(node);
		tree.updateUI() ;
		nodeTable.remove(user.toString());
	}
	
	public void updateUser(User user1)
	{
		MutableTreeNode node;
		node = (MutableTreeNode)nodeTable.get(user1.toString());
		
		if(user1.toString().equals(user.toString()))
			return;
			
		if(node == null ) {
			addUser(user1);
			return;
		}
		
		node.setUserObject(user1);
		nodeTable.put(user1.toString(),node);
		treeModel.reload(node);
		tree.updateUI() ;
		
		treeModel.nodeChanged(node);
	}
        
        public void createFrame(User user){
			frame.createFrame(user,false);
        }
 
        //Required by TreeWillExpandListener interface.
        public void treeWillExpand(TreeExpansionEvent e) 
                    throws ExpandVetoException {
        
        }
 
        //Required by TreeWillExpandListener interface.
        public void treeWillCollapse(TreeExpansionEvent e) {
        }
 
        // Required by TreeExpansionListener interface.
        public void treeExpanded(TreeExpansionEvent e) {
        }
 
        // Required by TreeExpansionListener interface.
        public void treeCollapsed(TreeExpansionEvent e) {
        }
    }   
}
//
//	MyRenderer: Tree cell rendering class
//
 
class MyRenderer extends DefaultTreeCellRenderer implements ChatConstants {
	final ClassLoader loader = ClassLoader.getSystemClassLoader();
	final ImageIcon rootIcon = new ImageIcon(loader.getResource("images/root.gif")),
					onlineIcon = new ImageIcon(loader.getResource("images/online.gif")),
					offlineIcon = new ImageIcon(loader.getResource("images/offline.gif")),
					busyIcon = new ImageIcon(loader.getResource("images/busy.gif")),
					idleIcon = new ImageIcon(loader.getResource("images/idle.gif"));
	private User user;
 
	public MyRenderer()
	{
 
	}
 
	public Component getTreeCellRendererComponent(
						JTree tree,
						Object value,
						boolean sel,
						boolean expanded,
						boolean leaf,
						int row,
						boolean hasFocus) {
 
		super.getTreeCellRendererComponent(
						tree, value, sel,
						expanded, leaf, row,
						hasFocus);
 
 
		if (leaf) {
			try{
			 user=getUser(value);
			}
			catch(Exception e)//for root node
			{
				System.out.println("Error message = " + e);
				setIcon(rootIcon);//ROOT
				setToolTipText(null);
				return this;
			}
			System.out.println(user.toString() + " has status = " + user.isOnline);
			switch(user.isOnline) {
				case ONLINE :
					setIcon(onlineIcon);//ONLINE USER
					setToolTipText("I am online @ "+user.hostname);
					break;
				case OFFLINE:
					setIcon(offlineIcon);//OFFLINE
					setToolTipText("Offline");
					break;
				case BUSY :
					setIcon(busyIcon);//BUSY USER
					//updateUI();
					setToolTipText("I am busy @ "+user.hostname);
					break;
				case IDLE :
					setIcon(idleIcon);//IDLE USER
					setToolTipText("Away from computer @ "+user.hostname);
					break;
				default:
					setIcon(offlineIcon);//OFFLINE
					setToolTipText("Offline");
				}
				setIcon(onlineIcon);
				
		} else {
			setIcon(rootIcon);//ROOT
			setToolTipText(null);
		}
 
		return this;
	}
	
		private User getUser(Object value)
	{
		
		DefaultMutableTreeNode node = (DefaultMutableTreeNode)value;
		User nodeInfo = (User)(node.getUserObject());
		return nodeInfo;
	}
 
}