JavaFx How to hide / show TreeItem - java

I have a TreeItem (a Tree with 100+ TreeItems elements).
I want to give the user the ability to hide (or show again) some TreeItems. (So the user will see just the items he care about).
I cant see a property of TreeItem which give me the ability to show or hide.
How can I do it ?

If you want to show/hide any TreeItem in TreeView you should add or remove it:
parentTreeItem.getChildren().remove(treeItem);
...
parentTreeItem.getChildren().add(treeItem);

Related

Remove Scrollbar from ComboBox JavaFX

I want to remove the scrollbar from the combobox and hence increase the height of the combobox when it is open. Meaning, i want to see all the items without scrolling.
Thanks!
If you don't want scrolling and the list options will remain few in number and relatively constant, you might actually want the ChoiceBox UI element. It has relatively the same function as the ComboBox without the scrolling capability.
...
choice boxes, the UI controls that provide support for quickly selecting between a few options.
Check out the Oracle docs on how to use them -- it's very similar to the ComboBox.

treeviewer does not deselect the tree item

I wanted to create dynamic tree struture in my application.
There my requirement is When the user selects any tree node
and click insert the child should be inserted below the selected parent. That is working fine. Now the problem is
i am unable to deselect the currently selected tree item in order to change tree item .
I clicked the background of the view and tried to insert
a new tree item, but still it goes to the previously selected tree node as still it highlight as the selected one. How to solve this issue?
If you create a TreeViewer with style SWT.SINGLE then the tree will insist on keeping an item selected unless you explicitly clear the selection.
To clear a selection use:
treeViewer.setSelection(StructuredSelection.EMPTY);
If you use SWT.MULTI as the tree style you can clear the selection with Ctrl+Click

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.

Add a checkbox to most parent tree element?

I want to add a checkbox widget to the most upper parent tree element like shown in http://www.smartclient.com/smartgwt/showcase/#tree_checkbox
How could I do this by just using GWT?
I only found that I can create a Tree*Item* with a checkbox and add it to the Tree:
TreeItem item = new TreeItem(new CheckBox("box"));
But how can I create a box for the Tree itself?
You need to assemble your own widget that extends gwt Composite class and wrap in it a tree/celltree , flowpanels and any widget you like to add like a select all checkbox.
You should be trying to use cell widget based CellTree - http://gwt.googleusercontent.com/samples/Showcase/Showcase.html#!CwCellTree
It is much more customizable and ensures better performance.

Show/Hide JMenuItems in a JPopupMenu based on a JTree's selection

I have a JTree which displays a JPopupMenu when I right click nodes in the JTree. What is the best way to hide/show or enable/disable certain JMenuItems in my JPopupMenu based on the node selected in the JTree?
The first solution that came to mind was to add a TreeSelectionListener to the JTree for the 'value changed' event. In the event handling code I would use the TreeSelectionEvent's getNewLeadSelectionPath() method to get the path of the most recent selection, and use the resulting TreePath object's getLastPathComponent() to get the selected node. From here I would have a series of IF statements that access my JPopupMenu object and perform the modifications necessary to hide/show specific JMenuItems.
However, something feels off about this, and so I decided I would ask SO if there was a better approach.
The way that I chose to tackle this within my own app was to use the "userObject" property of the DefaultMutableTreeNode class which allows you to just store any data you want along with your node. I have a variety of types of things that extend from an abstract base class which defines a "createPopupMenu()" method. Then, in the selection listener (just as you described in your question) I get the user object and ask it to create a popup menu appropriate for the selected object and display that.
Getting the selected tree node is straight forward and should work as you described it. To modify the popup menu I would recommend using Actions. This way you wouldn’t have to modify your live menu and could also add e.g. a JToolBar that contains the same actions that react the same way the items in your menu do.

Categories

Resources