• Post Reply Bookmark Topic Watch Topic
  • New Topic

Changing color of nodes in JTree

 
Dharmendra Kumar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Whenever mouse moves over the nodes in JTree i need to change its color (either forground or background). How to do this? If anybody knows please inform me.
Regards
Dharmendra Kumar
 
Chantal Ackermann
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
attach a MouseListener to the nodes in the tree and listen to the mouseEntered event. if the event is dispatched on a certain node, than it has to be rendered again in another color.
the easiest way to do this would be to switch the focus to this node. this way you can attach a renderer to the nodes that changes the color of the node when the focus changes.
 
Hetal Seth
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Check if this helps you.
MouseMotionListener mml = new MouseMotionAdapter()
{
public void mouseMoved(MouseEvent e)
{
JTree tree = (JTree) e.getComponent();
TreePath path = tree.getPathForLocation(e.getX(), e.getY());
int selRow = tree.getRowForLocation(e.getX(), e.getY());
if(selRow != -1) {
NodeObj nodeObj = NodeManager.getNodeObj(path.getLastPathComponent());
tree.setSelectionPath(path);
}
else if (selRow == -1)
tree.clearSelection();
}
};
jTree.addMouseMotionListener(mml);
Good luck,
Hetal Seth
[ August 02, 2002: Message edited by: Hetal Seth ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!