Select the TreeNode before (java swing, tree) - java

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:
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.

Related

JavaFX Get node by clicking on node

I want to get the node one column to the left of the node I clicked on. How can I do this? I’ve been trying all sort of things but I can’t figure it out...
Thanks!

Java: How to get the index of file in JTree

I have a JTree and I have it displaying everything I need, but i want to add what ever file i choose from the JTree to a JTextArea. I have this been done using a JList which all i do is get the selected index of the file in the JList and then append that to the JTextArea... But looking through the JTree there is no method to get the index of the chosen like in the JList, I have searched online and have had no luck with finding a solution!
The only way I can think of doing this a having a few methods to search through the JTree and at each level return where index of the file is, but that is alot of work so is there any better way to do this? Really what I am asking is what would be the best way to do this?
If you are only allowing single selection, you can use:
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
To get selected node. You could create a Map where the key is going to be the DefaultMutableTreeNode, and the associated value could be the String of the file you want to create.

Java: How to select all descendants of a given ancestor on the Jtree?

I would like to select an ancestor DefaultMutableTreeNode and get all descendant DefaultMutableTreeNode of this ancestor in the JTree.
I am using TreeSelectionListener to catch selection event on the current JTree.
Basically, what I would like to be able to do is, select an ancestor node, and be able to copy it's descendant tree into another ancestor.
You should be able to walk the subtree recursively through the children() of the DMTN.
FWIW:
Maybe this will help a little, but ExampleDepot is a good site for Java example code, and they have a lot of Swing examples.
Here is a link to their set of JTree examples. I hope you find what you need.
http://www.exampledepot.com/egs/javax.swing.tree/pkg.html

JTree node Rename

I am using a JTree and in this to raname a JTree node I am using right click(Rename through popup) or F2 key or double click. But problem is : when I rename a name and hit Enter key, node successfully renamed and when I rename a name and click anywhere on the frame(windows explorer style), node name does not rename.
Please give me a solution for this problem and Thanks in advance.
It's probably a little late, but the correct solution is to call JTree.setInvokesStopCellEditing(true). This will cause the tree to execute the rename when the user hits enter, or when the cell editor loses focus.
The ability to save the current edit by clicking elsewhere in the tree is not something that comes standard as part of the default JTree implementation. You're going to have to get into the details of how to use trees and do some special customization in order to achieve this functionality. This sounds like it would involve something with the focus of Swing components, i.e., when the tree gains focus, if the node is being edited, then save the node rename.

Creating Dynamic JTrees (Controlling Root Node Visibility)

I have a question about how to dynamically generate JTrees. Is there a way to set the Root Node invisible without making its children invisible too? I have tried to do the following but it shows all nodes as invisible. Keep in mind that I want to add and remove children of the Root Node at any point in time. I've added comments so you can follow what I intend to do. Let me know if they are doing something I dont need, as I am new to JTrees and don't know the conventions. I would also like to be able to select multiple children for the listener.
DefaultMutableTreeNode rootNode;
rootNode = new DefaultMutableTreeNode(); //I want this invisible.
DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
JTree tree = new JTree(treeModel);
treeModel.addTreeModelListener(this);
tree.setRootVisible(false); // Sets everything invisible
tree.setEditable(true); //makes tree dynamic
tree.setShowsRootHandles(true); //supposedly allows you to see the children of the nodes.
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
//I would like the line above to be multi-select; however, this doesn't seem to be an option.
DefaultMutableTreeNode table = new DefaultMutableTreeNode( "table1");
rootNode.add(book);
DefaultMutableTreeNode value = new DefaultMutableTreeNode( "value");
table.add(value);
In the above example. Nothing is shown and when I remove the "tree.setRootVisible(false)" everything is visible including the node.
A very late answer, but I have just had the same problem.
Ensure to expand your root node, so that its children become visible :
yourTree.expandPath(new TreePath(root.getPath()))
I'd say the difference between the code in the question and in the TreeDemo is that the tree demo creates and adds all its nodes before creating the actual tree. If the nodes are to be added dynamically (after the tree is created) it should be done through the TreeModel. Otherwise no events saying the tree has changed will be generated. At least that is what the tutorial seems to say about editing the node's "content", might be the same issue:
Note that although DefaultMutableTreeNode has methods for changing a
node's content, changes should go through the DefaultTreeModel cover
methods. Otherwise, the tree model events would not be generated, and
listeners such as the tree would not know about the updates.
Someone's solution
Works fine for me. I based my tests on the TreeDemo from the Swing tutorial on How to Use Trees. Compare your code with the tutorial code to see what the difference is.
A very late answer also, I'm a java beginner and had the same problem so it may help.
setRootVisible(false) also hides all nodes for me so I added setShowsRootHandles(true) to show all nodes :
tree.setRootVisible(false);
tree.setShowsRootHandles(true)
I hope it helps !
https://docs.oracle.com/javase/tutorial/uiswing/components/tree.html#display

Categories

Resources