I've the following problem: I created a tree viewer and bind a data model. The tree is comlete collapsed.
Now I want to select a specific node in the tree:
treeViewer.setSelection(new StructuredSelection(person), true);
Person is one of my custom objects in the data model. The node will be found and selected if the tree is expanded.
Because the node is a child of another node (3. level), nothing happens if the tree is collapsed.
Is it possible to select/focus the node, expand the parent items etc.?
I know I can recursively walk over all nodes and try to find the correct one but is there a method which do this work for me? Or maybe there's a different call of setSelection which let's me expand the tree path?
You can use TreeSelection for this. This takes aTreePath as an argument - which lists all the nodes in the path.
TreePath path = new TreePath(... array of nodes from root to person ...);
treeViewer.setSelection(new TreeSelection(path), true);
Related
It would be very useful to expand a JFace tree viewer in java swt as follows:
whenever a new node is added to the tree, the tree viewer expands only the currently added node in the tree (and its children if any).
I have used the expandToLevel(getIndex()) method, but this does not expands the children of the currently added node if any.
Thank you in advance for any hints and for your interest.
JTree jtree has a mouse listener attached to it.when the user clicks on a Jtree component node, I get it's row integer via
jtree.getRowForLocation(evt.getX(), evt.getY());
the problem is that the return row integer is different when there are jtree nodes which are expanded and when it's collapsed.
I need a way to somehow get a unique and absolute ID of each jtree nodes clicked so that there is no confusions.
Is there a way to add data to the Jtree component?
Otherwise, I need a way to listen to when nodes are expanded and recalculate the row returned from that above function....
In your current approach you are relying on the state of the tree view to identify the node. However, as you have found out the state of the view can change. What does not change is the tree model that is underlying the view.
If you need a unique ID for each node then you should add this directly to each tree node in your tree model. In the mouse listener you can then detect the node that you clicked on and access its unique ID.
Paths uniquely identify nodes, so getPathForLocation will give you the path, but that is a TreePath object, so if you have to have an integer represent the ID you would need to write a method that translates TreePaths to integers.
Actually I'm interested to create an inverted JTree in Java in such a way that root exists at top and its child nodes at the next level and so on. As we know in case of simple JTree the child nodes appears expanding on the right side of the parent node but I want to implement inverted JTree in my project where child nodes expands downwards which gives an appearance of "TREE" type of data structure.
As this is very essential for my project so can anyone suggest me the code for the above mentioned problem?
Thanks in advance.
Then JTree is not the correct choice. Perhaps, you should look into Java2D and stuff like that.
I have two JTrees instances (leftTree and rightTree). User can select some nodes from left tree and add the same to right tree. I have the below code in add button action listener to expand and select the node in rightTree after the node has been added.
rightTree.updateUI();
TreePath[] paths = leftTree.getSelectionPaths();
if (null != paths && paths.length > 0)
{
TreePath path = paths[0];
DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
rightTree.scrollPathToVisible(new TreePath(node.getPath()));
rightTree.setSelectionPaths(paths);
}
leftTree.clearSelection();
This code seems to work fine for some nodes but it cannot work for some other nodes in leftTree. The problem is even after the above code is executed, the rightTree is in collapsed state and I cannot see the selected node.
I have tried using other methods in JTree like setExpandsSelectedPaths(true), expandPath(new TreePath(node.getParent())). Also, tried calling rightTree.repaint() or rightTree.validate() after the above code is executed. But still the problem exists. But rightTree.isExpanded(new TreePath(node.getParent())) retruns true;
My tree is about 7-8 levels deep. Please help me to solve this and let me know if you need more information.
For me the approach is incorrect. TreePath is in fact sequence of nodes from the current node to top most parent. In other words TreePath from node is created by calling getParet() till tull is reached.
So if you get a node from one tree and create path the path is sequence of node in the original tree and the path is futile in the second tree because some nodes in the path just don't exists in the target tree model.
You need to get selected node and find an appropriate one in the second tree. Guess a node with the same user object. The for the found node in the target tree (if we found it) create TreePath and select/expand.
I have a tree and a TreePath to one of its nodes. How do I programatically select this node?
Will setSelectionPath of the JTree object alone work?
Yes. From the Java Documentation
If any component of the path is hidden
(under a collapsed node), and
getExpandsSelectedPaths is true it is
exposed (made viewable).