Add 'expand' button to JTree node that has no children? - java

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

Related

Getting a Boolean value JAVAFX

I have a group of nodes within a GridPane and have some listeners adding and removing those nodes from the GridPane.
I was wondering if there is a way for me to create a get method(Boolean) or such to test if the nodes are currently in the GridPane or not.
I want to enable the button when the nodes aren't in the gridpane aka false.
Any help/thoughts appreciated!
That's how you can check for the presence of a node:
gridpane.getChildren().contains(yourNode);
You can check if the parent of the Node node that you want to check is the GridPane. This should be a bit faster than using the child list, since it does not require iterating through the child list:
node.getParent() == gridPane
You can also use bindings to enable/disable the button (assuming there is a single node that decides, if the Button should be enabled or disabled)
button.disableProperty().bind(node.parentProperty().isEqualTo(gridPane));

Moving items in an SWT tree from parent to parent

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.

Root immediate child nodes icons not visible when hiding root

I have a JTree structure as shown below. I have Icons that appear whenever a node has child nodes, which is working properly.
My problem is that I need to hide the ROOT node. When I hide the ROOT node, the Icons for MainTop1 and MainTop2 nodes are not displayed, even though they have children. To hide ROOT node I'm using "setRootVisible(false)".
Also when ROOT node is hidden, the Icons for Topic1 and Topic nodes are displayed properly.
Anyone knows how to display the Icons for MainTop1 and MainTop2 when ROOT is hidden? Thanks in advance.
Below is my tree structure:
ROOT
MainTop1
Topic1
Subtopic1
Subtopic2
MainTop2
Topic2
Subtopic1
JTree#setShowsRootHandles(boolean newValue) controls the state of the handles for root elements.
Normally, this is false. Try changing to true
The order of the factors affects the product:
Create all nodes
Create model
Add nodes to model
Create JTree
setRootVisible(false)
setModel()
simple example

Partial Selection in JTree using checkbox for every node

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

Java: How to select all descendants of a given ancestor on the Jtree?

I would like to select an ancestor DefaultMutableTreeNode and get all descendant DefaultMutableTreeNode of this ancestor in the JTree.
I am using TreeSelectionListener to catch selection event on the current JTree.
Basically, what I would like to be able to do is, select an ancestor node, and be able to copy it's descendant tree into another ancestor.
You should be able to walk the subtree recursively through the children() of the DMTN.
FWIW:
Maybe this will help a little, but ExampleDepot is a good site for Java example code, and they have a lot of Swing examples.
Here is a link to their set of JTree examples. I hope you find what you need.
http://www.exampledepot.com/egs/javax.swing.tree/pkg.html

Categories

Resources