How to make JTree selection model only allow sibling selection? - java

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.

Related

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

Prefuse set different color to every nodes group

I am new to prefuse and don't understand several things regarding nodes.
How can I get an event raised whenever a node clicked?
How can I group similar nodes with the same color?
How can I keep an extra meta-data on every node?
Its important to say that I build the graph in real-time and not loading it from any file.
Thanks,
Ozrad.
Three answers to your three questions:
The best way to react on node clicks is to extend prefuse.controls.ControlAdapter and add it to the Display.
prefuse.action.assignment.DataColorAction assigns color based on a data field. For more advanced scenarios you can use ColorAction with predicates or extend the class DataColorAction.
You can add columns to the node table to store metadata. A column can also store objects if you need it:
vg.getNodeTable().addColumn("meta", MyMetaData.class);

Interactive multiway tree/graph applet with mouse listeners

I have a graph in which the nodes have 0 or more successors and 0 or more predecessors.
I want to make a visualization (preferably through JAVA) such that:
There should be a search box. If I enter the id of the node the node should appear on the screen.
If I left click a node, it's children should appear below the node(in tree representation).
If I right click the node then it's parent should appear above the node.
If I double click any node then it should have same effect as searching the node(as in point 1)
I want to know what approach should I follow (any package etc.)? Is there some similar implementation whose code I may reuse?

Creating a checkbox java list

i am trying to generate a checkbox list feeded by some records from a database just like the image (JAVA Swing), initially i tried with several chexkboxes but it didn't work, now i am trying with a multiple selection list but it didn't work as well and none of the answered questions here looks like to solve the specific needs i have.
Specifications:
Multiple selection list.
Every node of the list must have a checkbox object.
When checked every node must stay highlighted.
It must have an scrollbar if the content is bigger to the initially setted dimentions.
It must have an scrollbar if the content is bigger to the initially set dimensions.
Put the JList inside a JScrollPane.
When checked every node must stay highlighted.
This is going to confuse the user. The check marks are sufficient.
Every node of the list must have a check box object.
You'll have to extend a JList and a ListCellRenderer to gain this functionality. The ListSelectionListener that I have is over 100 lines of code.
You might find an already existing check box JList on the web. I have one in a book.

Allow dynamic drag drop permissions using arrays

I have created two trees in gwt-ext one contains the values of nodes that can be dragged and other the nodes which can be dropped on. The problem being i am not able to set only a particular set of leaves mentioned in the array, from a tree to be dragged on other tree while the leaves which are not in the array cannot be dragged. I have enabled drag drop on the tree and then manually allow a leaf to disable drag but its not working. Tried it using the setallowdrag() method and also using setTreeAttribute("allowDrag",false) but don't know why it just doesn't work. Please help.
TreeNode n22=treePanel.getNodeById("ynode-121");
n22.setTreeAttribute("allowDrag","false");
n22.setAllowDrag(false);

Categories

Resources