overriding JTree double-click to prevent node expansion? - java

It looks like there are 2 default mechanisms to expand a folder node in a JTree. One is to click on the expanded/collapsed icon next to a node. The other way is to double-click on the node itself.
Is there a way to stop this 2nd mechanism? I would like to override the double-click on a node so it does something (updates another display to show statistics on the node being double-clicked), and would like it to not expand/collapse the tree node. (just to be clear: I don't want to prevent the node from being expanded/collapsed, I just want to require the user to click on the expanded/collapsed icon.) How can I do this?

From the relevant API page, you would do something like this:
JTree tree = new JTree();
tree.setToggleClickCount(0);
This means you must use 0 clicks to expand a tree node, effectively disabling double click. This will not interfere with other methods of tree expansion.

Call setToggleClickCount(0) on the JTree
This will effectively disable expanding on double-click.

Related

JTree representing a graph

I am using the JTree Swing utility to represent a Tree that actually have loops. I have a single node called root, but some of the children will eventually point back to other parts of the tree, thus not making it a true tree, but rather a graph.
My Java application keeps locking up, (no exceptions being thrown, no stack overflow... etc) when I try to use the little gray arrows to expand and contract parts of the graph.
My question is, does JTree require that none of the DefaulMutableTreeNodes not contain a loop?
If so, how do we represent something like that using a JTree utility. For example, when you are debugging an application say in eclipse, and you can infinitely use the variable tree in debug mode to keep on looking through a looped object. That is the behavior I am looking for.
Any suggestions?
I don't think it's a problem that nodes in a Jtree loop on themselves. Apparently you have a problem only with an "expand all" button, which makes sense because an expand all method will go recursively through the nodes until they have no sons.
Jtree does not have an expand all button by default, so I'm guessing yours is already customized...? My suggestion would be either remove the button, or customize the code to stop the expansion if findind a node that was already expanded higher in the hierarchy.

Javafx: adding more context information to an event

I have an alternative tree implementation because Treeview did not do what I needed it to do. It works great except for one thing: event handling.
The tree consists of logical nested objects and each object is represented using a multitude of hbox, vbox, labels, etc.
When you use the tree, you want to register your event handlers on the root of the tree and intercept all the events, this works however there is not enough context information.
You might get a mouse click event from a certain instanced Label but no way of checking which item in the tree it corresponds to. When building the tree the containers for each object can intercept the events and "enhance" them to provide new context information but I'm not sure how to do this.
The copyFor() is useless as it is overriden again when you fire the event. So how can I take generic events like MouseEvent.ANY and KeyEvent.ANY and add a minimum amount of context? Am I forced to create my own events?
UPDATE
For example if we have the tree:
A
B
C
In GUI elements it could look like this:
VBox(A)
HBox(A)
Icon(A)
Label(A)
VBox(B)
HBox(B)
Icon(B)
Label(B)
VBox(C)
...
All event handler can be registered on the root VBox of A here. However suppose someone clicked on the Label(B) element, the root listener would only see that the original element is a Label. However as the listener, you want to know that it's linked to B.
IMO you can utilize the Node.setUserData() method of the label/container/treeitem/tree for keeping its related context data. Then use it first by getting the source and target nodes of desired event. Otherwise, you may have to create your own events.

defaultTreeModel nodes removed still shown in table

I am trying to remove nodes from a DefaultTreeModel. If I try to remove a single node, it doesn't get removed from the GUI. If I remove 2 nodes, only 1 of them is getting removed, etc.
I am using removeNodeFromParent(node); to remove the nodes.
Could someone please help me with this problem.
It sounds like your tree isn't detecting the nodesWereRemoved event that should be triggered by the removeNodeFromParent(node); method.
Are you using the standard DefaultTreeModel and JTree objects? If so, they should automatically refresh when doing a removal. If you've written a custom of either class, you're probably not handling the nodesWereRemoved event (in your custom JTree), or you're not triggering the event (in your custom DefaultTreeModel).
If you're using the standard objects, try calling JTree.revalidate(); and JTree.repaint(); after you remove the node, to force the tree to be repainted from your model change.
A final suggestion, have you tried any of the removeXXX() methods of JTree instead?

jTree refresh component after creating new jTree

I read many topics about this but still have some problems.
I'm using java desktop project from NB. I have created tree from palette and now after every click of button I want to create new tree and refresh it. So I have event action performed where I want make new jTree add some DefaultMutableTreeNode's and show this in window. Any ideas?
Maybe in other words how should I create Jtree to modify it content? I make something like this now:
in initComponents jTree1 = new JTree(nodeF);
where nodeF is my field (DefaultMutableTreeNode) initialised before initComponents
and then I want to modify this node element adding and removing another nodes.
I'm able to refresh tree ((DefaultTreeModel) jTree1.getModel()).reload(); but I'm unable to create new instance of nodeF
I fell like I'm making some stupid mistake.. dont know how to create gui right..
First idea, get away from Netbeans. Using a GUI editor prevents you from learning important parts of Swing, and generates code that is awful to debug or customize.
Second idea, it sounds like you could get away with just refreshing the existing tree and removing the current content by setting the root. That way you don't have to create a new tree each time.
Is the tree structure actually changing? Or are you just expanding/collapsing nodes in the tree? My guess is the latter.
You should probably change your data model objects to implement TreeNode. The JTree will query your TreeNode objects as needed to determine which ones have children, what the children are, etc.
To expand/contract nodes in the tree without using the built-in tree controls, use the methods in JTree, e.g. expandPath or expandRow.

Is there a way to have expander icons for multiple roots in a JTree?

I have a JTree with multiple "roots" (Of course, I actually have an invisible real root with multiple children).
The nodes expand and collapse on double click, but there's no visual indication that you can do this as there is no expander icon.
This is made worse by the fact that the tree is collapsed by default, but expanding the "roots" doesn't really help, as each has many children and it would look cluttered.
Is there a way to display the expander icons without making the real (and utterly valueless) root visible?
Any other suggestions to make the display clearer welcome.
Would tree.setShowsRootHandles(true) be a good way to display those "expander icons" ?
A tree typically also performs some look-and-feel-specific painting to indicate relationships between nodes. You can customize this painting in a limited way.
First, you can use tree.setRootVisible(true) to show the root node or tree.setRootVisible(false) to hide it.
Second, you can use tree.setShowsRootHandles(true) to request that a tree's top-level nodes — the root node (if it is visible) or its children (if not) — have handles that let them be expanded or collapsed.
Check also your look and feel to be sure what the renderer does with your tree.

Categories