Partial Selection in JTree using checkbox for every node - java

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

Related

Getting a Boolean value JAVAFX

I have a group of nodes within a GridPane and have some listeners adding and removing those nodes from the GridPane.
I was wondering if there is a way for me to create a get method(Boolean) or such to test if the nodes are currently in the GridPane or not.
I want to enable the button when the nodes aren't in the gridpane aka false.
Any help/thoughts appreciated!
That's how you can check for the presence of a node:
gridpane.getChildren().contains(yourNode);
You can check if the parent of the Node node that you want to check is the GridPane. This should be a bit faster than using the child list, since it does not require iterating through the child list:
node.getParent() == gridPane
You can also use bindings to enable/disable the button (assuming there is a single node that decides, if the Button should be enabled or disabled)
button.disableProperty().bind(node.parentProperty().isEqualTo(gridPane));

How to expand a TreeItem till its last leaf node

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

treeviewer does not deselect the tree item

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

CheckBox Node Tree

I have a checkbox node tree using java Swing.
My objective is to select all the check box nodes on the click of a button .
i.e
Pseudo Code :
on(button click){
Check all the check box nodes
}
Code :
for (CheckBoxNode node_r : checkBoxRows)
{
node_r.setSelected(true);
frame.repaint();
}
checkBoxRows is a list containing all the parent nodes in the check box node tree .
Can you guide me the correct way of doing this ?
checkBoxRows is a list containing all the parent nodes
1) no needed that, because JTree implements own TreeModel, only if is there extra Controler, then you have to implements Vector<Object>, because TreeModel is TwoDimensional by default, and List has only one dimension
2) you can simle loops inside TreeModel with checking for JCheckBox and if returns true/false from method isSelected
3) examples for JTree, TreeModel, JTree Renderer

Add 'expand' button to JTree node that has no children?

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

Categories

Resources