CheckBox Node Tree - java

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

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 can I tell what nodes are highlighted on a displayed JTree?

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
}
});

Building a Complete Check Box Node Tree

My objective is to build a check box node tree with the following features .
Nodes :
There will be 2 types of nodes all over the tree .
1 . Parent Nodes
2 . Child Nodes
All the nodes are check box nodes .
The parent nodes need to have an additional folder like icon , in addition to the check box.
And all the check boxes must be editable
More than one check box must be checkable at a time .
What is the best possible approach that i can take ?
Looks similar to this Stack Overflow result:
JTree with checkboxes
Which refers to this example: http://www.java2s.com/Code/Java/Swing-JFC/CheckBoxNodeTreeSample.htm
In this latter example, change nonLeafRenderer to return your new component (a folder with a checkbox, possibly in a JPanel.)
If you want a ready to go component you can use CheckBoxTree from Jide-oss.

Partial Selection in JTree using checkbox for every node

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

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