Building a Complete Check Box Node Tree - java

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.

Related

How to get the levels to which the treeviewer is expanded?

lets consider below Treeviewer data,
Project
->package1
->package2
->->class1
The Viewer has been expanded to level-2. But, how to know that to what level the viewer has been expanded.
I am aware of setting the viewer to expand to a particular level by expandToLevel() method.
Is there a way to get the maximum level to which the viewer has been expanded?
I don't think there is a simple way to get this information.
You could use the getExpandedTreePaths method and find the longest TreePath that is returned. But note that the JavaDoc for this says:
Returns a list of tree paths corresponding to expanded nodes in this
viewer's tree, including currently hidden ones that are marked as
expanded but are under a collapsed ancestor.
which might not be what you want.

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

Root immediate child nodes icons not visible when hiding root

I have a JTree structure as shown below. I have Icons that appear whenever a node has child nodes, which is working properly.
My problem is that I need to hide the ROOT node. When I hide the ROOT node, the Icons for MainTop1 and MainTop2 nodes are not displayed, even though they have children. To hide ROOT node I'm using "setRootVisible(false)".
Also when ROOT node is hidden, the Icons for Topic1 and Topic nodes are displayed properly.
Anyone knows how to display the Icons for MainTop1 and MainTop2 when ROOT is hidden? Thanks in advance.
Below is my tree structure:
ROOT
MainTop1
Topic1
Subtopic1
Subtopic2
MainTop2
Topic2
Subtopic1
JTree#setShowsRootHandles(boolean newValue) controls the state of the handles for root elements.
Normally, this is false. Try changing to true
The order of the factors affects the product:
Create all nodes
Create model
Add nodes to model
Create JTree
setRootVisible(false)
setModel()
simple example

Expanding tree nodes

I am new to eclipse RCP, well as a matter of fact to java also. Have a very basic question.
I have a JFace TreeViewer. I want to expand a particular node in that. The catch is I only have the name of the node. and no information apart from that.
I tried using treeItem, compared its string with that of the node name that I have, thus I got the node. I tried expanding it in the contentprovider of the tree. But i am not getting the desired output. When I check it in the log i get that it is expanded but it doesnt show in the viewer. I am performing this in display.asyncExec method in the contentprovider.
I hope the question is clear.
JFace viewers were created so that developers wouldn't have to mess around with SWT widgets and could use higher level API instead. When using JFace's viewer/content provider/label provider you should be in control of what nodes are in your tree (thus when you say you only know the name, I assume you are using SWT Tree directly). You can read about JFace viewers from Eclipse help.
To expand a tree node use expandToLevel(Object elementOrTreePath, int level) method of TreeViewer (the level is relative to the node that is expanded, not the root of the tree).

How to make JTree selection model only allow sibling selection?

I have a jtree and I only want the user to be able to select sibling nodes. If the user selects nodes that aren't siblings, I want the previously selected nodes to be deselected.
The user should also be able to select sibling nodes that aren't contiguous. I was hoping to extend defaulttreeselectionmodel.
Is there a simple way to do this?
Seems like you'll have to play with TreeSelectionModel (which will store current selection state) and TreeSelectionListener. An example algorithm could be the following : when your selection change, if your TreeSelectionListener is not in active mode, it enters into that mode (this is useful as your listener will update selection in some cases, and you don't want crazy cycles, want you ?). Then, it checks selected nodes using TreeSelectionModel#getSelectionPaths(). If this array size is one, only one node is selected and all is OK. If its size si greater than 1, then you'll have to write some code to ensure those nodes are siblings (as an example by ensuring their TreePath are identical except the last part.

Categories

Resources