Results 1 to 5 of 5
  1. #1
    Preethi is offline Member
    Join Date
    Jan 2008
    Posts
    83
    Rep Power
    0

    Unhappy Node selection in jtree

    I have created a tree.Now,i want to get co-ordinates or the location of
    all visible node when expanded...I tried to get the count of all nodes of only selected
    node and also the co-ordinates...But i don't know how to take all the co-ordinates of
    the visible nodes...Can anyone help me...

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package test;
    
     import java.awt.BorderLayout;
     import java.awt.Graphics;
     import java.awt.Rectangle;
     import java.util.Hashtable;
     import javax.swing.JApplet;
     import javax.swing.JPanel;
     import javax.swing.JScrollPane;
     import javax.swing.JTree;
     import javax.swing.UIManager;
     import javax.swing.event.TreeSelectionEvent;
     import javax.swing.event.TreeSelectionListener;
     import javax.swing.tree.DefaultMutableTreeNode;
     import javax.swing.tree.TreePath;
     /** * * @author 8563 */
     public class reg extends JApplet
     {
         private JTree tree;
         private Drawing drawing;
         int count;
         public DefaultMutableTreeNode createNodes()
         {
             DefaultMutableTreeNode rootnode = new DefaultMutableTreeNode("RESOURCE");
             DefaultMutableTreeNode resource1 = new DefaultMutableTreeNode("RESOURCE1");
             DefaultMutableTreeNode resource2 = new DefaultMutableTreeNode("RESOURCE2");
             DefaultMutableTreeNode resource3 = new DefaultMutableTreeNode("RESOURCE3");
    
             rootnode.add(resource1);
             rootnode.add(resource2);
             rootnode.add(resource3);
             resource1.add(new DefaultMutableTreeNode("task1"));
             resource1.add(new DefaultMutableTreeNode("task2"));
             resource1.add(new DefaultMutableTreeNode("task3"));
             resource2.add(new DefaultMutableTreeNode("task1"));
             resource2.add(new DefaultMutableTreeNode("task2"));
             resource2.add(new DefaultMutableTreeNode("task3"));
             resource3.add(new DefaultMutableTreeNode("task1"));
             resource3.add(new DefaultMutableTreeNode("task2"));
             resource3.add(new DefaultMutableTreeNode("task3"));
             return rootnode;
         }
         public void init()
         {
             try
             {
                 UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
             }
             catch (Exception evt)
             {
                 evt.printStackTrace();
             }
             drawing = new Drawing();
             tree = new JTree(createNodes());
             tree.addTreeSelectionListener(drawing);
             JPanel p =  new JPanel();
             p.setLayout(new BorderLayout());
             p.add(tree, BorderLayout.WEST);
             p.add(drawing, BorderLayout.CENTER);
             getContentPane().add(new JScrollPane(p));
         }
         private  class Drawing extends JPanel implements TreeSelectionListener
         {
             JTree tree = new JTree();
    
             public String selectedParent = " ";
             private String selectedChildValue = "";
             private int selectedChildY = -1;
             private Hashtable nodeStates = new Hashtable();
             Object EXPANDED = new Object();
             boolean parentselected = false;
             TreePath path;
            @Override
            public void paintComponent (Graphics g)
            {
                super.paintComponent(g);
                int x = 30;
                           
                if(isExpanded(path)== false)
                {
                    g.drawString(" paint",5,selectedChildY);
                }
                if (!selectedChildValue.equals("Resource1"))
                {
                    g.drawString("Currently selected child: " + selectedChildValue + ".",70 , selectedChildY-25);
                    repaint();
                }
               
            }
             public void valueChanged(TreeSelectionEvent e)
             {
                 DefaultMutableTreeNode node = (DefaultMutableTreeNode)e.getPath().getLastPathComponent();
    
                 boolean Path = e.isAddedPath();
                 count= node.getChildCount();
                 if(count > 0)
                 {
                     parentselected = true;
                 }
                 System.out.println("node count  is : " + count);
                 System.out.println("userobj : " + node.getUserObject());
                 if(e.isAddedPath() == true)
                 {
                    // repaint();
                 }
                 if (Path == true)
                 {
                     selectedChildValue = (String)node.getUserObject();
                     JTree tree = (JTree)e.getSource();
                     Rectangle bounds = tree.getPathBounds(e.getPath());
                     Rectangle rt = tree.getVisibleRect();
                     System.out.println("visible"+ rt.x);
                     selectedChildY = bounds.y + bounds.height;
                 }
                 else
                 {
                     selectedChildValue = "";
                     selectedChildY = -1;
                 }
                 if(!node.isRoot())
                 {
                     selectedParent  = (String)node.getUserObject();
                     JTree  tree = (JTree)e.getSource();
                     Rectangle bounds = tree.getPathBounds(e.getPath());
                     try
                     {
                        selectedChildY = bounds.y + bounds.x;
                     }
                     catch(NullPointerException ex){}
    
                 }
                
             }
             public boolean isExpanded( TreePath path)
             {
    
                 if (path== null)
                     return false;
                 Object state = nodeStates.get(path);
                 if ((state == null) || (state != EXPANDED))
                     return false;
                 TreePath Path = path.getParentPath();
                 if(Path!= null)
                     return isExpanded(Path);
                 return true;
             }
         }
         
         public int isParent(TreePath path)
         {
             if(path.getParentPath().getPathCount()>0 && tree.getVisibleRowCount()>0)
             
                count = path.getPathCount();
                return count;
         }
     }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Preethi is offline Member
    Join Date
    Jan 2008
    Posts
    83
    Rep Power
    0

    Default

    Yes...the expanded nodes

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    I'm not clear one thing. As the visibility you get the coordinates of a rectangle. It's always 0, because it gives the result reference to the applet.

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    //  <applet code="NodeLocTest" width="400" height="400"></applet>
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import javax.swing.tree.*;
    
    public class NodeLocTest extends JApplet
                             implements TreeSelectionListener {
        Rectangle locater = new Rectangle();
    
        public void valueChanged(TreeSelectionEvent e) {
            TreePath path = e.getNewLeadSelectionPath();
            if(path == null)
                return;
            DefaultMutableTreeNode node =
                (DefaultMutableTreeNode)path.getLastPathComponent();
            JTree tree = (JTree)e.getSource();
            int row = tree.getRowForPath(path);
            Rectangle r = tree.getRowBounds(row);
            System.out.printf("row = %2d  r = [%2d, %3d, %d, %d]  node = %s%n",
                               row, r.x, r.y, r.width, r.height, node);
            locater.setBounds(r);
            tree.repaint();
        }
    
        public void init() {
            JTree tree = new JTree() {
                protected void paintComponent(Graphics g) {
                    super.paintComponent(g);
                    Graphics2D g2 = (Graphics2D)g;
                    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                        RenderingHints.VALUE_ANTIALIAS_ON);
                    g2.setPaint(Color.red);
                    g2.draw(locater);
                }
            };
            int mode = TreeSelectionModel.SINGLE_TREE_SELECTION;
            tree.getSelectionModel().setSelectionMode(mode);
            tree.addTreeSelectionListener(this);
            tree.addTreeWillExpandListener(willCall);
            add(tree);
        }
    
        private TreeWillExpandListener willCall = new TreeWillExpandListener() {
            JTree tree;
    
            public void treeWillCollapse(TreeExpansionEvent e) {
                checkLocation(e);
            }
    
            public void treeWillExpand(TreeExpansionEvent e) {
                checkLocation(e);
            }
    
            private void checkLocation(TreeExpansionEvent e) {
                if(tree == null)
                    tree = (JTree)e.getSource();
                int changeRow = tree.getRowForPath(e.getPath());
                int selectedRow = tree.getLeadSelectionRow();
                if(changeRow < selectedRow)
                    locater.setSize(0,0);
            }
        };
    }

Similar Threads

  1. JTree Programmatic Node Expansion and Selection Probelm
    By hemanthjava in forum AWT / Swing
    Replies: 3
    Last Post: 01-16-2013, 07:23 AM
  2. Replies: 7
    Last Post: 05-09-2008, 07:54 AM
  3. How to Transmit data from one node to another
    By swimberl in forum Networking
    Replies: 2
    Last Post: 01-04-2008, 08:48 PM
  4. JTree trouble
    By Alantie Vala in forum AWT / Swing
    Replies: 3
    Last Post: 07-31-2007, 11:12 PM
  5. how to display data in Jtree
    By paty in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 12:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •