How to get JTree expanded?
I have a wizard with several screens where user has to fill his/her details for further processing. At the second screen I have a radio group with three radio buttons that enable additional elements. To proceed, user has to choose one of them. When user selects third button, single-selection JTree
filled in with data enables and user has to select an option from it. Then user has to press "Next" to get to next screen. The option he\she had selected is stored as a TreePath
. So far so good.
My problem is the following. If the user wants to come back from the following screen to the screen with a JTree
, I want to provide him\her with the JTree
expanded to the option that had been selected and to highlight the option. However, whatsoever I try to do for that (any combinations of expandPath
, scrollPathToVisible
, addSelectionPath
, makeVisible
) always provides me with a collapsed tree. I try to expand both leaves and nodes. My code looks like this:
rbProcessJTree.setSelected(isProcessJTree());
if (null != getSelectedTablePath()){
trTables.addSelectionPath(getSelectedTablePath());
trTables.expandPath(getSelectedTablePath());
trTables.scrollPathToVisible(getSelectedTablePath());
}
When setSelected()
is called, state change listener is invoked that enables JTree
. The model is loaded during the form initialization.
Each time I switch between screens, I save the input data from previous screen and dispose it. Then, when I need to open previous screen back, I save data from the following screen, dispose it, load data to this screen and show it. So each time the screen is generating from scratch.
Could you please explain, what sequence of operations has to be done to get JTree
expanded in a newly created form,with data model loaded and selection path provide开发者_如何学运维d?
Here is some code I've used to record expansion and selected paths in JTree. You could record the state stuff here between Next and Back pages.
// record expanded nodes to reinstate later;
TreePath rootPath= new TreePath(objectWeKnowIsAtRoot);
Enumeration<TreePath> expandedPaths = tree.getExpandedDescendants(rootPath);
// record current-selection - it's OK if it's null
TreePath selectedPath = tree.getSelectionPath();
// ******* do stuff that may, and often will, modify our tree content ********
// restore valid expansion paths (some may - correctly - have been removed in modifications)
while (expandedPaths != null && expandedPaths.hasMoreElements())
{
TreePath path = expandedPaths.nextElement();
if (isPathValid(path))
{
tree.expandPath(path);
}
}
// restore selected path, if apt
if (isPathValid(selectedPath))
{
tree.setSelectionPath(selectedPath);
}
The isValidPath() method used above is based on this one:
http://forums.sun.com/thread.jspa?threadID=567157&tstart=38085
If you're using DefaultMutableTreeNode
, you can always use an Enumeration
to search for the TreeNode
you want. Then setSelectionPath()
should work if getExpandsSelectedPaths()
is true.
Edit: I was thinking more like using an Enumeration
from DefaultMutableTreeNode
:
JTree tree = new JTree();
DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree.getModel().getRoot();
...
private TreePath find(DefaultMutableTreeNode root, String s) {
Enumeration<DefaultMutableTreeNode> e = root.depthFirstEnumeration();
while (e.hasMoreElements()) {
// return match
}
return null;
}
Then you can do this:
TreePath path = find(root, "pizza");
tree.setSelectionPath(path);
tree.scrollPathToVisible(path);
精彩评论