gpt4 book ai didi

java - 如何扩展JTree?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:41:12 26 4
gpt4 key购买 nike

我有一个带有多个屏幕的向导,用户必须在其中填写他/她的详细信息以进行进一步处理。在第二个屏幕上,我有一个单选组,其中包含三个启用其他元素的单选按钮。要继续,用户必须选择其中之一。当用户选择第三个按钮时,填充数据的单选JTree 启用,用户必须从中选择一个选项。然后用户必须按“下一步”才能进入下一个屏幕。他/她选择的选项存储为 TreePath。到目前为止一切顺利。

我的问题如下。如果用户想从以下屏幕返回到带有 JTree 的屏幕,我想为他/她提供扩展到已选择选项的 JTree并突出显示该选项。但是,无论我为此尝试做什么(expandPathscrollPathToVisibleaddSelectionPathmakeVisible 的任何组合)总是为我提供了一棵折叠的树。我尝试同时展开叶子和节点。我的代码如下所示:

rbProcessJTree.setSelected(isProcessJTree());

if (null != getSelectedTablePath()){
trTables.addSelectionPath(getSelectedTablePath());
trTables.expandPath(getSelectedTablePath());
trTables.scrollPathToVisible(getSelectedTablePath());
}

setSelected() 被调用时,状态改变监听器被调用以启用 JTree。模型在表单初始化期间加载。

每次在屏幕之间切换时,我都会保存上一个屏幕的输入数据并进行处理。然后,当我需要打开前一个屏幕时,我从下一个屏幕保存数据,处理它,将数据加载到这个屏幕并显示它。所以每次屏幕都是从头开始生成的。

请您解释一下,必须执行哪些操作顺序才能使 JTree 以新创建的形式展开,加载数据模型并提供选择路径?

最佳答案

这是我用来在 JTree 中记录扩展和选定路径的一些代码。您可以在此处记录下一页和后页之间的状态。

// 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);
}

上面使用的isValidPath()方法是基于这个:

http://forums.sun.com/thread.jspa?threadID=567157&tstart=38085

关于java - 如何扩展JTree?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2955914/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com