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.
Related
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.
I have a JList where each element holds some special data. So I have implemented AbstractListModel for the JList.
Everything works great when I have one or more elements in the list.
But, does not work in the scenario below:
Jlist is already instantiated with some elements.
I need to remove all the elements from the list.
I do not have the JList object(Component) accessible in the model, so can't call repaint().
I cannot call fire***() methods of AbstractListModel since it throws OutOfIndexException.
Any suggestions?
You state:
Because I am calling fireContentsChanged(this, 0,0)..which will look for 0th element in the list which does not exist. I know the cause but not the solution.
Don't call fireContentsChanged(...) when removing items. Call fireIntervalRemoved(...) instead. Also as an aside, you should never have to call repaint() in this situation. Changing the state of the model and then notifying the model's listeners through a call to the proper fireXXX(...) method is all that should be done.
I have a JTree that stores "ShipmentItem"s and the .toString() on them shows the quantity, then the name of the ShipmentItem. At some point I change the quantity of multiple items at once but the toString() doesn't refresh until I've actually clicked on that particular tree node. I don't want to have to extend JTree to use 'Property Fired' I just want to be able to refresh it so that it shows the update.
I tried jtree.setModel(tree.getModel()) this didn't seem to work at all.
When you change something to your TreeModel (which you do by adjusting the ShipmentItem objects) you must make sure your TreeModel fires the correct event. This will cause the JTree to repaint the correct part. If you for example started from the DefaultTreeModel, your extension should call nodeChanged when the object of the node has changed.
Note: you do not have to adjust the toString method for correct rendering. The concept you are looking for is a TreeCellRenderer (check the Swing tutorial for more information)
I have an app that uses a JFace TreeViewer. I have it hooked up to a ContentProvider. Mostly, it works great. However, for some actions, like adding a Node in the middle of a list of Nodes or changing a value which should change the label for a Node, the refresh() call doesn't work. I've tried including the parent Node, say "true" for label update. Nothing works all the time.
I have seen that if I leave a collapsible Node closed and add and then expand, the added Node is shown. But if it's already expanded, no change is shown. If I save my tree info to disk and look, the change is made. It's just the TreeViewer refresh that is not working.
I looked at the inputChanged method in my ContentProvider, but it is only called at the beginning and end of my app execution.
Can anyone help? I've read all the web pages that even hint at an answer and nothing has worked.
The inputChanged() if the contentprovider shouldn't be called during refresh() - it is only called when a setInput() call is made to the tree viewer.
In worst case you could call setInput() with the original input to make your elements refreshed, but it can be too slow for your application.
Basically, when you add a new Node in the middle of the group, you have to refresh the parent node (the node who returns the added element using the getChildren() call), or one of its parents. Basically calling refresh() without any parameters might work for this reason.
So for testing, I suggest that you should call refresh() without any parameters, try, whether it works or not, and if it works, then try to figure out the most specific node that works with your application.
Maybe this is obvious to you, but are you calling the refresh from the UI thread? Specifically try the following:
Display.getCurrent().asyncExec(new Runnable() {
#Override
public void run() {
// TODO Call refresh() here
}
});
There is also a syncExec() method.
Hope this helps!
Did you apply a label decorator to your TreeViewer?
I just edited an entry in my TreeViewer without the LabelDecorator I applied before and update(selectedItem, null) actually applied the update (it did nothing before).
Perhaps it's a jface bug?
[edit] seems you have to use updateLabel() from your DecoratingLabelProvider object.
Unfortunately, nothing I could find resolved this. I ended up closing the file and reopening it to force a "refresh/update". Thanks all for the advice.
For me the add(), update() or remove() functions worked pretty decent, of course it is annoying to do so manualy... but if you call them from your model on a change it should just work fine.
I have a JTree which I give objects that implement the TreeNode interface, and a custom TreeModel to display them (not using DefaultMutableTreeNode). I would like to change the text colour of some nodes. I can't find anything in the docs, except javax.swing.tree.DefaultTreeCellRenderer.setTextNonSelectionColor(Color newColor), but it will change everything indiscriminately, and I only need it for some nodes (specifically, broken links, i.e. nodes whose corresponding files can't be found on the disk, should be greyed out, the rest should be default). Can it be done, and how?
You are close to your answer. What you need to do is Sub Class the DefaultTreeCellRenderer and override a few of the DefaultTreeCellRenderer's methods. Then make sure you tell the tree to use your custom cell renderer.
What you will need to do is have some state variables that indicate whether or not a link is broken, and set the color of the node based on that.
You might also look at org.netbeans.swing.outline, mentioned in this answer. Ordinary extensions of TableCellRenderer and the RenderDataProvider interface make it especially easy to customize the appearance of rows in the tree.