I have a TreeViewer wherein when the user selects a TreeItem and clicks on a button 'Expand', the entire hierarchy under the TreeItem shoul be opened till the last leaf node is found.
I have found an existing method on TreeViewer which would address my requirement i.e
viewer.expandToLevel(item, level);
I have the selected item which I would pass as first argument to the above method, but how do I get the level?
Use the special value AbstractTreeViewer.ALL_LEVELS for this:
viewer.expandToLevel(object, AbstractTreeViewer.ALL_LEVELS);
Note: object is your data model object (from your content provider) not a TreeItem
Related
I'm working on an app that displays a JTree. I want the user to be able to highlight certain nodes and then do things to those nodes after they press a button.
Highlighting already works - they can click on things, and then shift-click or control click to highlight other things. is there any way to detect which nodes are highlighted in this way?
Thanks!
If I'm understanding you correctly you want to find all the tree nodes that a user has selected. See JTree.getSelectionPaths(). The Javadoc states that it returns
An array of TreePath objects indicating the selected nodes, or null if nothing is currently selected
Each TreePath
represents an array of objects that uniquely identify the path to a node in a tree. The elements of the array are ordered with the root as the first element of the array
As #MadProgrammer states in the comment the last object in the array is the selected node.
You can create a TreeSelectionListener as follows and use e.paths from the TreeSelectionEvent:
jTree.addTreeSelectionListener(new TreeSelectionListener() {
#Override
public void valueChanged(TreeSelectionEvent e) {
// e.paths has the selected nodes in the TreeModel
}
});
I wanted to create dynamic tree struture in my application.
There my requirement is When the user selects any tree node
and click insert the child should be inserted below the selected parent. That is working fine. Now the problem is
i am unable to deselect the currently selected tree item in order to change tree item .
I clicked the background of the view and tried to insert
a new tree item, but still it goes to the previously selected tree node as still it highlight as the selected one. How to solve this issue?
If you create a TreeViewer with style SWT.SINGLE then the tree will insist on keeping an item selected unless you explicitly clear the selection.
To clear a selection use:
treeViewer.setSelection(StructuredSelection.EMPTY);
If you use SWT.MULTI as the tree style you can clear the selection with Ctrl+Click
I have a application which uses a JTree. For each node in the tree, there is a checkbox attached to the node using a customized TreeCellRenderer. When some items of a node are selected i.e not all children of a node are selected using the chechboxes, how do i indicate using the checkbox of the parent node that only a partial list of nodes is selected. For this, i need to draw a little square in the checkbox indicating that it is partially selected.
You can use e.g.
http://www.roseindia.net/javatutorials/tristatecheckbox.shtml or
http://www.javaspecialists.co.za/archive/Issue082.html
I am tryin to make a software in which when clicked on the subnode of a jtree, detecting which node is selected, want to display respective message.
You will need to add a TreeSelectionListener to the model containing your nodes. The event arguments contain data for the node(s) selected.
Here is some example documentation.
I'd like to add the 'expand' button to my JTree's nodes to indicate that they are expandable. The catch is that they have no children until the user clicks on them (due to processing that happens in the background).
Is there any way I can set a node as a parent or having children without it actually having children?
Thanks
It's possible using your own DefaultMutableTreeNode implementation overriding isLeaf():
Returns true if this node has no children.
Swing Tutorial: JTree explains it under 4.1 Dynamic Tree.
Have a fake child/child count and replace it with real children using TreeWillExpandListener