Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Jeanne Boyarsky
Ron McLeod
Sheriffs:
Paul Clapham
Liutauras Vilda
Devaka Cooray
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
Swing / AWT / SWT
JTree TreeNode question
Tal Tal
Ranch Hand
Posts: 40
posted 20 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
hi
how do i make a TreeNode in a jtree to extand ?
Craig Wood
Ranch Hand
Posts: 1535
posted 20 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Assuming you are asking how to do this programatically vis–a–vis with the mouse
import java.awt.*; import java.awt.event.*; import java.util.Enumeration; import javax.swing.*; import javax.swing.tree.*; public class ExpansionTest { TreeNode selectedNode; public ExpansionTest() { String[] names = new String[] { // branches |<-- child leaf nodes -->| "hawks", "gray", "red-tailed", "rough-legged", "falcons", "harrier", "kestrel", "kite", "owls", "barred", "saw-whet", "snowy" }; JTree tree = createTree(names); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(getUIPanel(tree, names), "North"); f.getContentPane().add(tree); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } private void findNode(JTree tree, String name) { selectedNode = null; TreeNode root = (TreeNode)tree.getModel().getRoot(); findNode(root, name); } private void findNode(TreeNode node, String name) { String s = node.toString(); if(name.equals(s)) selectedNode = node; if(node.getChildCount() >= 0) { Enumeration e = node.children(); while(e.hasMoreElements()) { TreeNode n = (TreeNode)e.nextElement(); findNode(n, name); } } } private void expandNode(JTree tree, TreeNode node, boolean expand) { if(node.isLeaf()) return; if(node.getChildCount() >= 0) { Enumeration e = node.children(); while(e.hasMoreElements()) { TreeNode n = (TreeNode)e.nextElement(); expandNode(tree, n, expand); } } TreePath path = new TreePath(((DefaultMutableTreeNode)node).getPath()); if(expand) tree.expandPath(path); else tree.collapsePath(path); } private JTree createTree(String[] names) { DefaultMutableTreeNode root = new DefaultMutableTreeNode("birds"); DefaultMutableTreeNode[] nodes = new DefaultMutableTreeNode[names.length]; for(int j = 0; j < nodes.length; j++) nodes[j] = new DefaultMutableTreeNode(names[j]); for(int j = 0; j < 9; j += 4) { root.insert(nodes[j], j % 3); for(int k = j + 1; k < j + 4; k++) nodes[j].insert(nodes[k], k - j - 1); } DefaultTreeModel model = new DefaultTreeModel(root); return new JTree(model); } private JPanel getUIPanel(final JTree tree, String[] names) { final JComboBox combo = new JComboBox(names); final JButton expand = new JButton("expand"), collapse = new JButton("collapse"); ActionListener l = new ActionListener() { boolean open; public void actionPerformed(ActionEvent e) { JButton button = (JButton)e.getSource(); String s = (String)combo.getSelectedItem(); if(button == expand) open = true; if(button == collapse) open = false; findNode(tree, s); expandNode(tree, selectedNode, open); } }; expand.addActionListener(l); collapse.addActionListener(l); JPanel panel = new JPanel(); panel.add(expand); panel.add(combo); panel.add(collapse); return panel; } public static void main(String[] args) { new ExpansionTest(); } }
Look ma! I'm selling my stuff!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
creating jTree from objects
Refreshing JTree
JTree class
update treenode's parent in jtree
jtree problem
More...