0

I am looking for a way to select the TreeNode before the actually selected node (if no Node is selected, the first Node AFTER the root should be selected) on Button click. I am basically having a Hashtable ht and a JTree filled with nodes (all Nodes allowing Children). Once i click a node, i want to look up in the Hashtable for that Object and the system should write something like "You selected the 'Object.name'". Then the user should be able to click a JButton and select the node before the clicked node. Of course there should also be this line with an updated 'Object.name'. Maybe there is even a way to update the GUI of the tree, so that the other TreeNode is shown as selected.

I have been trying around for quite a while with the treePath-Methods of the selectionModel and the TreeNodes (e.g. saving them in Lists and trying to select the Element before), but nothing seems to work. I am also not finding anything useful on Google or StackOverflow. I was thinking that it might work somehow with the getPreviousSibling-Method from the DefaultMutableTreeNode?

Last thing I tried:

DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode();
treeNode = (DefaultMutableTreeNode) getLastSelectedPathComponent();
setSelectionPath(new TreePath(((DefaultTreeModel) treeModel).getPathToRoot(treeNode.getPreviousSibling())));

Unfortunately I keep getting a NullPointerException:

NullPointerException in line 2 of this snippet (although I selected another TreeNode before, wich has a sibling before).

Can anyone please tell me what I did wrong or tell me what other way might work? :D If you need more information please have no regard in asking me.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Rüdiger
  • 893
  • 5
  • 27
  • 56
  • Welcome to StackOverflow! Some users are unable to follow links to images. If you would like assistance, please copy the code directly into your question by using the [edit](http://stackoverflow.com/posts/34857521/edit) link above. – OneCricketeer Jan 18 '16 at 15:17
  • Thank you, edited the question now. – Rüdiger Jan 21 '16 at 08:23

0 Answers0