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.