Results 1 to 5 of 5
Thread: Node selection in jtree
- 06-19-2008, 08:28 AM #1
Member
- Join Date
- Jan 2008
- Posts
- 83
- Rep Power
- 0
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; } }
- 06-19-2008, 10:09 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
Visible nodes means, expanded nodes in the tree?
- 06-19-2008, 11:11 AM #3
Member
- Join Date
- Jan 2008
- Posts
- 83
- Rep Power
- 0
Yes...the expanded nodes
- 06-19-2008, 11:23 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
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.
- 06-20-2008, 12:25 AM #5Java 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
-
JTree Programmatic Node Expansion and Selection Probelm
By hemanthjava in forum AWT / SwingReplies: 3Last Post: 01-16-2013, 08:23 AM -
how to create Popup Menu with Sub Menu while right-clicking the JTree Node??
By Kabiraa in forum AWT / SwingReplies: 7Last Post: 05-09-2008, 08:54 AM -
How to Transmit data from one node to another
By swimberl in forum NetworkingReplies: 2Last Post: 01-04-2008, 09:48 PM -
JTree trouble
By Alantie Vala in forum AWT / SwingReplies: 3Last Post: 08-01-2007, 12:12 AM -
how to display data in Jtree
By paty in forum New To JavaReplies: 1Last Post: 07-24-2007, 01:28 AM
Bookmarks