Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
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 01-16-2008, 10:27 PM
Member
 
Join Date: Dec 2007
Posts: 18
hemanthjava is on a distinguished road
JTree Programmatic Node Expansion and Selection Probelm
I've read plenty of java Swing forums or questions and none of them have the right answers. I am trying to dynamically select a node in a JTree by searching for it using a search panel based on its property.

My jtree allows single selection model. (SINGLE_TREE_SELECTION).

Code:
jTree.setExpandsSelectedPaths(true); // jTree.expandPath(treePath); jTree.setSelectionPath(treePath); jTree.scrollPathToVisible(treePath);

I know the node is being selected and a call to jTree.getSelectionPath() shows the expected treePath. The problem is that graphically the treePath is neither expanded nor the specific searched node is highlighted.

TreePath is correct, and the node is being selected. The branch is just not being expanded and the node is not being highlighted. The node is not highlighted even when the branch is already in an expanded state.

Can anyone please help.

I am using Java 1.5

Thanks,
Bob
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 01-17-2008, 08:36 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,266
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.event.*; import java.util.Enumeration; import javax.swing.*; import javax.swing.tree.*; public class NodeExpansion implements ActionListener { JTree tree = new JTree(); public void actionPerformed(ActionEvent e) { String s = e.getActionCommand(); setExpandedState(s); } private void setExpandedState(String id) { DefaultMutableTreeNode root = (DefaultMutableTreeNode)tree.getModel().getRoot(); Enumeration e = root.breadthFirstEnumeration(); while(e.hasMoreElements()) { DefaultMutableTreeNode node = (DefaultMutableTreeNode)e.nextElement(); if(node.getUserObject().equals(id)) { TreePath path = new TreePath(node.getPath()); if(node.isLeaf()) { DefaultMutableTreeNode parent = (DefaultMutableTreeNode)node.getParent(); expandNode(new TreePath(parent.getPath())); } else { expandNode(path); } tree.setSelectionPath(path); break; } } } private void expandNode(TreePath parent) { TreeNode node = (TreeNode)parent.getLastPathComponent(); if (node.getChildCount() >= 0) { Enumeration e = node.children(); while(e.hasMoreElements()) { TreeNode n = (TreeNode)e.nextElement(); TreePath path = parent.pathByAddingChild(n); expandNode(path); } } tree.expandPath(parent); } private JPanel getContent() { JPanel panel = new JPanel(new GridLayout(1,0)); panel.add(new JScrollPane(tree)); panel.add(getRightComponent()); return panel; } private JScrollPane getRightComponent() { ButtonGroup group = new ButtonGroup(); JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(2,5,2,0); gbc.weightx = 1.0; gbc.anchor = GridBagConstraints.WEST; gbc.gridwidth = GridBagConstraints.REMAINDER; TreeModel model = tree.getModel(); DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot(); Enumeration e = root.breadthFirstEnumeration(); while(e.hasMoreElements()) { DefaultMutableTreeNode node = (DefaultMutableTreeNode)e.nextElement(); String s = node.getUserObject().toString(); JRadioButton rb = new JRadioButton(s); rb.setActionCommand(s); rb.addActionListener(this); group.add(rb); panel.add(rb, gbc); } return new JScrollPane(panel); } public static void main(String[] args) { JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(new NodeExpansion().getContent()); f.setSize(500,400); f.setLocation(200,200); f.setVisible(true); } }
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 09:54 AM
How to Transmit data from one node to another swimberl Networking 2 01-04-2008 10:48 PM
Gridbag Layout Probelm hemanthjava AWT / Swing 3 12-04-2007 07:12 PM
Dom4j probelm with clearContent method aorteu New To Java 0 11-23-2007 05:36 PM
Communication with Expansion Slot Compact Flash device on PDA percivalwcy CLDC and MIDP 0 07-25-2007 11:04 AM


All times are GMT +3. The time now is 02:31 PM.


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