LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-01-2004, 01:44 AM   #1
decadent
LQ Newbie
 
Registered: Mar 2004
Posts: 5

Rep: Reputation: 0
Java: Some questions about JTree


First of all - sorry for my bad English.

There is tree with root "Towns" and leafs "Minsk", "Brest", "Grodno", "Vitebsk".
Code:
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Towns");
DefaultMutableTreeNode child = new DefaultMutableTreeNode("Minsk"); root.add(child);
child = new DefaultMutableTreeNode("Brest"); root.add(child);
child = new DefaultMutableTreeNode("Grodno"); root.add(child);
child = new DefaultMutableTreeNode("Vitebsk"); root.add(child);

model = new DefaultTreeModel(root);
tree = new JTree(model);
There is button "x", if you press it, the name of current (selected) node and his parent will be printed.

Code:
JButton x = new JButton("x");
x.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event) {
DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
System.out.println("Child:\n" + selectedNode + "\nParent:"+ selectedNode.getParent());
}});
How do set this values (name of current node and his parent) to the String variables.
I try write something like this: "String zxc = (String) (selectedNode.getParent());" but it does not work.

One more question.
How to interchange two neighbouring nodes?
For example, node "Grodno" is selected, I push "x" and "Grodno" now the second, and "Brest" - third.

Please, write code, if it is not difficult for you.

Last edited by decadent; 04-01-2004 at 09:19 AM.
 
Old 04-01-2004, 05:32 AM   #2
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 55
You need to call getUserObject() on your tree node. For example, if you created you treenode with
Code:
DefaultMutableTreeNode node = new DefaultMutableTreeNode("England"); // just an example!
then to retrieve england you need to call:
Code:
String myString = (String)node.getUserObject();
because the treenode can hold any sort of user data object, so you need to cast it back to be a string.
What you have at the moment:
Code:
System.out.println("Child:\n" + selectedNode + "\nParent:"+ selectedNode.getParent());
Does not work because you're just printing the memory address of the parent node. What you should have is something like:
Code:
System.out.println("Child:\n" + (String)selectedNode.getUserObject() + "\nParent:"+ (String)(selectedNode.getParent()).getUserObject());
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with JTree. Diederick Programming 1 06-17-2005 07:16 PM
Jtree can only modify one? ::JAVA:: logicdisaster Programming 0 09-27-2004 12:40 PM
How do i refresh a jtree? logicdisaster Programming 1 09-23-2004 03:33 PM
Java: JTree & drag'n'drop decadent Programming 1 06-07-2004 06:41 AM
2 Questions: java calling system commands? PERL vs Java? randomx Programming 28 11-28-2003 08:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration