重写JTree双击以防止节点扩展?

时间:2021-02-08 12:36:42

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.

看起来有两个默认机制来扩展JTree中的文件夹节点。一种方法是单击节点旁边的展开/折叠图标。另一种方法是双击节点本身。

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?

有办法阻止这第二种机制吗?我希望覆盖对节点的双击,以便它做一些事情(更新另一个显示以显示双击的节点上的统计信息),并希望它不展开/折叠树节点。(明确一点:我不想阻止节点展开/折叠,我只想要求用户单击展开/折叠图标。)我该怎么做呢?

2 个解决方案

#1


26  

From the relevant API page, you would do something like this:

从相关API页面中,您可以做如下操作:

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.

这意味着必须使用0单击来展开树节点,有效地禁用双击。这不会干扰其他树的扩张方法。

#2


3  

Call setToggleClickCount(0) on the JTree

在JTree上调用setToggleClickCount(0)。

This will effectively disable expanding on double-click.

这将有效地禁用双击扩展。

#1


26  

From the relevant API page, you would do something like this:

从相关API页面中,您可以做如下操作:

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.

这意味着必须使用0单击来展开树节点,有效地禁用双击。这不会干扰其他树的扩张方法。

#2


3  

Call setToggleClickCount(0) on the JTree

在JTree上调用setToggleClickCount(0)。

This will effectively disable expanding on double-click.

这将有效地禁用双击扩展。