Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-19-2008, 08:28 AM
Member
 
Join Date: Jan 2008
Posts: 78
Preethi is on a distinguished road
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...

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; } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-19-2008, 10:09 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,065
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Visible nodes means, expanded nodes in the tree?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-19-2008, 11:11 AM
Member
 
Join Date: Jan 2008
Posts: 78
Preethi is on a distinguished road
Yes...the expanded nodes
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-19-2008, 11:23 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,065
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
(Close on September 4, 2008)

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-20-2008, 12:25 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,146
hardwired is on a distinguished road
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); } }; }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to create Popup Menu with Sub Menu while right-clicking the JTree Node?? Kabiraa AWT / Swing 7 05-09-2008 08:54 AM
JTree Programmatic Node Expansion and Selection Probelm hemanthjava AWT / Swing 1 01-17-2008 07:36 AM
How to Transmit data from one node to another swimberl Networking 2 01-04-2008 09:48 PM
JTree trouble Alantie Vala AWT / Swing 3 08-01-2007 12:12 AM
how to display data in Jtree paty New To Java 1 07-24-2007 01:28 AM


All times are GMT +3. The time now is 08:14 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org