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
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Swing / AWT / SWT
TreePath from path?
Max Power
Ranch Hand
Posts: 47
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
hi, how can i get a valid treepath (jtree) out of a path like this:
c:\programs\test1\test2\....
thanks a lot
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
I like...
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Moving to the Swing/AWT Forum...
[Jess in Action]
[AskingGoodQuestions]
Craig Wood
Ranch Hand
Posts: 1535
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import java.awt.*; import javax.swing.*; import javax.swing.tree.*; public class PathTest { public PathTest() { String filePath = "c:/jexp/tutorial/images/dukeWaveRed.gif"; DefaultMutableTreeNode node = buildNodeFromString(filePath); DefaultMutableTreeNode lastLeaf = node.getLastLeaf(); TreePath path = new TreePath(lastLeaf.getPath()); System.out.println("path = " + path); DefaultTreeModel model = new DefaultTreeModel(node); JTree tree = new JTree(model); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(tree); f.setSize(300,300); f.setLocation(200,200); f.setVisible(true); } private DefaultMutableTreeNode buildNodeFromString(String path) { String[] s = path.split("/"); DefaultMutableTreeNode node, lastNode = null, root = null; for(String str : s) { if(str.indexOf(":") != -1) str = str.substring(0, str.indexOf(":")); node = new DefaultMutableTreeNode(str); if(root == null) root = node; if(lastNode != null) lastNode.add(node); lastNode = node; } return root; } public static void main(String[] args) { new PathTest(); } }
Consider Paul's
rocket mass heater
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
TreeExpansionListener
Initially expanded JTree?
JTree - expanding nodes
Node Selection in JTree
JTree - keeping expanded nodes expanded after deleting a node
More...