• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Changing color of nodes in JTree

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic