Highlighting nodes in a JTree

Hi,
I'd like to know how I can produce the efect of highlighting nodes in a JTree when the mouse moves over the JTree.
Thanks

You say to get the mouseevent coordinates with a mouse
listener, but which component should be the caller of
the methos addMouseListener?
If I choose the jtree, I can't get the 'mouseevent
coordinates' each time the mouse moves over a node,
because the methods mouseEntered and mouseExited are
invoked just once (when the mouse enter inside the
jtree's area (mouseEntered), and when the mouse gets
out the jtree's area(mouseExited)), so I can't change
the boolean field to true for nodes which have the
mouse over.just use mouseMoved of the MouseMotionListener.
however, i have my doubts that you will have a lot of fun with the suggested method (correct though it is).
at least make sure you only call the repaint method if the mouse moved into a new node. you might want to try the the nodeChanged method, that way you don't repaint the whole tree every time.
thomas

Similar Messages

  • JTree de-highlight node

    I have a JTree with DefaultMutableTreeNode-s
    I am using a SINGLE_SELECTION_MODE model
    When I use the mouse to select a node in the JTree, it automatically highlights that node. What should I use if I want to be able to use the mouse to de-select the node and remove the highlight?
    -andrew

    CTRL-click the node to de-select it.

  • How to highlight a node in a JTree???

    I am trying to highlight (actually set the Font to Bold) one node of a JTree when an TreeSelectionEvent occurs (valueChanged method). The node to be highlighted can be different than the one selected.
    Thanks for your help.
    JavaSan

    To control the appearance of tree nodes, you need to write a TreeCellRenderer. Check Sun's tutorial on how to use trees (there's a link to it in the API documentation for JTree). The signature of the method you need to implement is this:
    public Component getTreeCellRendererComponent(
        JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus)so as you can see, you can vary the appearance of the node based on any of the parameters of that method.

  • Highlighting nodes in JTree

    Hey guys please help me for this.
    I want to generate a Tree. In which after some action a few nodes are selected.
    Now i want to highlight those selected node in the Tree. Is it possible ??
    Is anyone have any clue how to do that.
    Thanks in advance

    well, even I'm facing this issue in my application but i did a small workaround. I am sure there should be a better way of achieving this, which i hope someone will post in this thread.
    Use Html tags in displaying the highlighted node:
    DefaultMutableTreeNode node = new DefaultMutableTreeNode ("<html><b>node1</b></html>");
    Pankaj

  • How do i expand all the nodes in a jtree

    Hi,
    I am working on a project where i need to expand all the nodes of a jtree i have tried a few different ways but it never seems to expand all the nodes..
    I would be very greatful if someone could point me in the right direction
    cheers
    Mary

    you could use the following method that expands nodes recursively
    expandNode( myTree, myRootNode, new TreePath( myRootNode ) );
    public static void expandNode( JTree tree, TreeNode node, TreePath path ) {
        tree.expandPath( path );
        int i = node.getChildCount( );
        for ( int j = 0; j< i; j++ ) {
            TreeNode child = node.getChildAt( j );
            expandNode( tree, child , path.pathByAddingChild( child ) );
    }

  • How to get total number of nodes in a JTree?

    Hi,
    I am trying to get total number of nodes in a JTree, and cannot find a way to do it.
    The current getRowCount() method returns the number of rows that are currently being displayed.
    Is there a way to do this or I am missing something?
    thanks,

    How many nodes does this tree have?
    import java.awt.EventQueue;
    import javax.swing.*;
    import javax.swing.event.TreeModelListener;
    import javax.swing.tree.*;
    public class BigTree {
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    TreeModel model = new TreeModel() {
                        private String node = "Node!";
                        @Override
                        public void valueForPathChanged(TreePath path,
                                Object newValue) {
                            // not mutable
                        @Override
                        public void removeTreeModelListener(TreeModelListener l) {
                            // not mutable
                        @Override
                        public boolean isLeaf(Object node) {
                            return false;
                        @Override
                        public Object getRoot() {
                            return node;
                        @Override
                        public int getIndexOfChild(Object parent, Object child) {
                            return child == node ? 0 : -1;
                        @Override
                        public int getChildCount(Object parent) {
                            return 1;
                        @Override
                        public Object getChild(Object parent, int index) {
                            return node;
                        @Override
                        public void addTreeModelListener(TreeModelListener l) {
                            // not mutable
                    JFrame frame = new JFrame("Test");
                    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    frame.getContentPane().add(new JScrollPane(new JTree(model)));
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
    }But for bounded tree model using DefaultMutableTreeNode look at bread/depth/preorder enumeration methods to walk the entire tree. Or look at the source code for those and adapt them to work with the TreeModel interface.

  • How to use custom nodes in a JTree without reinventing the wheel?

    Hello,
    Each node contains two JTextAreas in a Box layout and a few JButtons.
    I wish to display these nodes in a JTree.
    Reading the tutorial, it seems I would have to reimplement the TreeModel, TreeCellRenderer/Editor, MutableTreeNode interfaces etc.
    Can I use the DefaultTreeModel, and other standard widgets (great stuff!) to minimize the amount of reimplementation I must do. aka avoid reinventing the wheel? I was thinking of extending my node from the DefaultMutableTreeNode class - however it does not display the nodes with the TextAreas, buttons etc.
    any help appreciated.
    thanks,
    Anil

    was able to fix it over here:
    http://forum.java.sun.com/thread.jspa?messageID=4089777

  • Can the name of a specific node in a JTree be returned by MouseEvent?

    I have created a mouseListener for a JTree. When a node in the JTree is clicked it should display the name of the node in a JOptionPane. Currently, I have the following:
    tree.addMouseListener(new MouseAdapter()
    public void mouseClicked(MouseEvent me)
         if(me.getClickCount() % 2 == 0)
         String s = "Blank";
         s = (me.getSource()).toString();
         JOptionPane.showMessageDialog(null, "Double clicked "+ s, "two", JOptionPane.PLAIN_MESSAGE);               
    }//mouseClicked          
    });//MouseListener
    This gives me the class X Y value, border, maxsize, etc.... when a node is double clicked.
    Does anyone know of a better way?

    Don't use MouseListener.
    Instead, make yourself a TreeSelectionListener as follows:
    public class WhererverYourTreeIs implements TreeSelectionListener
         public void valueChanged(TreeSelectionEvent e)
             TreePath path = e.getPath();
             System.out.println(path.getLastPathComponent());
         public void initStuff()
                 tree.addTreeSelectionLIstener(this);
    }

  • Sorting the nodes of a JTree

    Can anyone tell me how to go about sorting the nodes in a JTree?

    The best way to do it is thru the treemodel. Either adjust the order in a DefaultTreeModel, or create your own TreeModel.

  • How do I get a dotted line to connect nodes in a JTree?

    I am trying to recreate a Windows Explorer application, does anyone know how they get the dotted lines to connect the nodes in the JTree????

    JTree uses a specific line style to represent the edges between nodes. The default is no edges, but we can set JTree�s lineStyle client property so that each parent node appears connected to each of its child nodes by an angled line:
    myJTree.putClientProperty("JTree.lineStyle", "Angled");
    We can also set this property such that each tree cell is separated by a horizontal line:
    myJTree.putClientProperty("JTree.lineStyle", "Horizontal");
    To disable the line style:
    myJTree.putClientProperty("JTree.lineStyle", "None");
    As with any Swing component, we can also change the UI resource defaults used for all instances of the JTree class. For instance, to change the color of the lines used for rendering the edges between nodes as described above, we can modify the entry in the UI defaults table for this resource as follows:
    UIManager.put("Tree.hash", new ColorUIResource(Color.lightGray))
    Hope this serves your purpose.
    Regards,
    Sachin Shanbhag

  • How can I get a count of ALL nodes in a JTree?

    Not sure if I am missing something here or what. Is there an easy way to determine the total number of nodes in a JTree? I thought there would be a method to return this, but I'm not seeing it in JTree or DefaultTreeModel. Do I have to start at the root and recursively get child counts? Yuck!
    Jamie

    You are absolutely right! Create a recursive method and count all your children from the root.
    Denis Krukovsky
    http://dotuseful.sourceforge.net/

  • Dynamically changing the color of nodes in a JTree

    I have created a JTree and want to dynamically change the color of some of the TreeNodes as my program runs. The JTree is displayed in a JPanel. There is an algorithm built into which identifies the nodes whose color I need to change. No user actions is performed (everythign is internal to the program).
    It seems that in the TreeCellRender only kicks in when the tree is first displayed. How do I get it to re-render the tree when the TreeModel changes? It seems that the Listeners are only looking for external user interactions.
    Any help would be greatly appreciated.
    Thanks!

    I think I was a bit too vague in my question. Let me try again...
    I have changed an attribute in a node in a JTree. This attribute is changed after the tree was initially rendered, but while the program is still running. I want to tell the TreeCellRenderer to look again at this node since that attribute that was changed will effect how the node should be renderered. I tried using the nodeChanged() method, but it did not work (the colot of the node did not change). Any advise how I can do this?
    Thanks!

  • Getting TreePath for all nodes in a JTree

    Hi everybody
    I want to get TreePath for all the nodes in a JTree
    Can anybody help me?

    Hi,
    i tried to used your code.
    there is a problem, it is not taking the entire paths.
    so if you have some children,it takes the paths only for the fathers.
    so i correct it to the following way(it will take the entire paths):
    public void getPathForAllNodes(TreePath path)
                  Object node = path.getLastPathComponent();
                  pathArrayList.add(path);
                  TreeModel model = tree.getModel();
                  if(model.isLeaf(node))
                  return;
                  int num = model.getChildCount(node);
                  for(int i = 0; i < num; i++)
                       //pathArrayList.add(path);
                       getPathForAllNodes(path.pathByAddingChild(model.getChild(node, i)));
             }thanks for your code

  • Can I move nodes in a jTree without the mouse?

    I want to move nodes in a jTree as if I was dragging and dropping but I don�t want to use the mouse. Is this possible and how.

    I want to move nodes in a jTree as if I was dragging
    and dropping but I don�t want to use the mouse. Is
    this possible and how.sure it's possible through the power of Java(tm)

  • Events of Nodes in a JTree

    Okay, perhaps I am sidetracked today by all the sad craziness occuring in New York and DC, or perhaps I just don't have a grasp on JTrees yet...but...
    I have implemented a TreeCellRender to create panels with buttons and other information to be the nodes of a JTree I created. Everything shows up beautifully, however, I want the buttons on this panel to depress and perfom actions (work like normal) when the user clicks/selects such a button in the tree structure. I can't figure out how to properly set up event handlers to get from the tree to the tree node to the "bean" I'm using as a node. If anyone has any helpful hints, I'd appreciate it!
    Thanks in advance!

    add actionlistener to the JTree, and then get the object by getLastPathComponent, and compare it if its the button and do the action.

Maybe you are looking for

  • Cursor problem in dark background

    Hi, I've changed my terminal bg to black, but i have problem to see text cursor espesialy when display brighness is low, i have to really try to find it when i want select text

  • Equalization and Symbol Rate

    Despite my number of posts on this board...I swear that I am happy with my NI devices.... I noticed a peculiar thing today.  I am using some of the example programs to do some channel characterization.  The investigation is meant to be relatively sim

  • Seagate FreeAgent External Hard Drive & Time Machine

    I have a 1 TB Seagate FreeAgent Go external hard drive that I use for Time Machine backups.  However, I get one or two backups on it and it suddenly becomes "Read only" and won't allow Time Machine to write to it.  My user name is listed as having re

  • Why MUST I download attachments on the iPhone ???

    I love my iPhone 3GS/32 ... but one thing drives me CRAZY: The iPhone is the ONLY device on which you can´t disable the automatic download of attachments. I travel a lot ... and pay hundreds of Euros for "data-roaming". It´s a either-or-decision: Eit

  • Viewing from thinkpad through TV

    How do I change the frequency on my T30 to view slides and videos on my harddrive through my TV?  I connect the thinkpad to the VGA slot on my TV.  The sound is OK.  But I cannot view any pictures.  The TV screen is showing blurred images. The freque