Moving items in an SWT tree from parent to parent - java

I want to move a TreeItem with all of its child nodes from one parent to another. But I found no method for that.
Is really the only way to do this by removing the TreeItem and rebuild it at the new position?
That would be quite time-consuming because my node can have a lot of children and/or subtrees so I have to make a recursion...

In order to change the parent of a TreeItem your only option is to dispose of the item at its original position and re-create the item with its new parent.
As Greg mentioned, if you have a data model, you may want to use a TreeViewer which does the re-parenting for you.
Performancewise this isn't a problem, unless you have millions of items. But with such figures you would already run into performance problems while populating the tree initially.

Related

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

Change SWT TreeItem parent

I am making a program in Java and I have a SWT tree widget that the user is supposed to toy with via drag and drop.
Now the problem is that when the user drags and drops a branch, I want to change the branch item's parent to whichever treeItem it was dropped on, but there doesn't appear to be any way to do so. I could simply create a new treeItem, but there is no easy way to transfer the children, so I need to redefine all the children, and their grandchildren and so on recursively. It seems pretty klunky and inefficient to me that I need to remake the entire branch just to change the parent.
Is there any clean way to do this?
Well, the clean way is to separate view/widgets from model (as in MVC). In case of SWT, you should use TreeViewer and implement ITreeContentProvider. jFace will take care of creating tree nodes for you and you only need to update your model and refresh the viewer.

How can I reset a JTree?

I'm currently working with JTree, more precisely with CheckBoxTree, an inherited class created by JIDE. I need to find a way to reset the tree, meaning :
Clearing the selection
Erasing the nodes
I tried unsetting the Tree variable, the treeModel, and refreshing the UI, but it doesn't works.
Any ideas ?
Unsetting the variable alone won't help - all that will do is lose your copy of the reference to the JTree object.
What you need to do remove the reference the containing object holds to the JTree - I assume you have it some kind of GridContainer or Layout object - remove it from the parent ojbect and then call updateUI on that object.
For erasing the nodes you should get the tree model and clear it or set a new tree model.
To clear the selection, call clearSelection() on the tree (note that setModel(...) already calls clearSelection() so if you want to do both together, just set a new model and repaint).
Just rebuild the tree at the beginning of each iteration.
Something of this sort
RootNode=new CheckBoxTreeNode("root");
CheckBoxTree= new CheckBoxTree(RootNode);
would do the trick. No need to call updateUI. Hope this help.

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

Creating Dynamic JTrees (Controlling Root Node Visibility)

I have a question about how to dynamically generate JTrees. Is there a way to set the Root Node invisible without making its children invisible too? I have tried to do the following but it shows all nodes as invisible. Keep in mind that I want to add and remove children of the Root Node at any point in time. I've added comments so you can follow what I intend to do. Let me know if they are doing something I dont need, as I am new to JTrees and don't know the conventions. I would also like to be able to select multiple children for the listener.
DefaultMutableTreeNode rootNode;
rootNode = new DefaultMutableTreeNode(); //I want this invisible.
DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
JTree tree = new JTree(treeModel);
treeModel.addTreeModelListener(this);
tree.setRootVisible(false); // Sets everything invisible
tree.setEditable(true); //makes tree dynamic
tree.setShowsRootHandles(true); //supposedly allows you to see the children of the nodes.
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
//I would like the line above to be multi-select; however, this doesn't seem to be an option.
DefaultMutableTreeNode table = new DefaultMutableTreeNode( "table1");
rootNode.add(book);
DefaultMutableTreeNode value = new DefaultMutableTreeNode( "value");
table.add(value);
In the above example. Nothing is shown and when I remove the "tree.setRootVisible(false)" everything is visible including the node.
A very late answer, but I have just had the same problem.
Ensure to expand your root node, so that its children become visible :
yourTree.expandPath(new TreePath(root.getPath()))
I'd say the difference between the code in the question and in the TreeDemo is that the tree demo creates and adds all its nodes before creating the actual tree. If the nodes are to be added dynamically (after the tree is created) it should be done through the TreeModel. Otherwise no events saying the tree has changed will be generated. At least that is what the tutorial seems to say about editing the node's "content", might be the same issue:
Note that although DefaultMutableTreeNode has methods for changing a
node's content, changes should go through the DefaultTreeModel cover
methods. Otherwise, the tree model events would not be generated, and
listeners such as the tree would not know about the updates.
Someone's solution
Works fine for me. I based my tests on the TreeDemo from the Swing tutorial on How to Use Trees. Compare your code with the tutorial code to see what the difference is.
A very late answer also, I'm a java beginner and had the same problem so it may help.
setRootVisible(false) also hides all nodes for me so I added setShowsRootHandles(true) to show all nodes :
tree.setRootVisible(false);
tree.setShowsRootHandles(true)
I hope it helps !
https://docs.oracle.com/javase/tutorial/uiswing/components/tree.html#display

Categories

Resources