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
}
});
Related
Is there anyway I can collapse a certain number of nodes in a graph using graphstream? I have tried adding ui.hide attribute,but that is not what I want. Ideally I would want a + sign on the first node that is collapsed and then when I click on the + sign then the rest of the nodes should appear. Can anyone help me with this? TIA
Not exactly sure I got it right, but I guest you want to fold/unfold a tree, a bit like a package/file system view on a IDE (eclipse, netbean...).
If I am right then there are some tricks that can help you do that in GraphStream.
The ui.hide attribute, as you mentioned, used on nodes and edges.
The layout.frozen attribute that blocks the layout algorithm on the selected node and allows you to control its position. You may want to set the position of the "folded" right at the clicked node's position in order to give the "folding" visual feeling.
The ui.class attribute that allows you to assign CSS classes to nodes, and thus give a particular style to node folded/unfolded nodes (e.g. add a "plus" sign icon).
In order to get notified when a node is clicked you need to retrieve a ProxyPipe from the viewer and listen to modifications coming from that viewer. especially the ui.clicked attribute that is automatically assigned to nodes that get clicked.
ProxyPipe fromViewer = viewer.newViewerPipe();
fromViewer.addSink(new SinkAdapter(){
#Override
public void nodeAttributeAdded(String sourceId, long timeId, String nodeId, String attribute, Object value) {
if(attribute.equals("ui.clicked")){
// Being notified that a node was clicked...
}
}
});
Finally, you have to decide which node to collapse. I guess a simple iterator rooted at the clicked node is what you need.
// let n be the clicked node...
Iterator<Node> it = n.getBreadthFirstIterator(true);
while(it.hasNext()){
Node m = it.next();
for(Edge e : m.getLeavingEdgeSet()) {
e.setAttribute("ui.hide");
}
if(n != m) {
m.setAttribute("ui.hide");
}
}
Then the Layout algorithm will be troublesome regarding nodes positions and stabilisation limits but that not really important.
You'll find a working example in this gist where clicked nodes collapse their sub-trees :
https://gist.github.com/pigne/2d4c0cbe8583f8a8f6b03309becaff3f
Here is an example tree graph before collapsing sub-trees:
Same graph with node 13 and 15 collapsed after clicking:
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
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 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.
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.