overriding JTree double-click to prevent node expansion?

时光毁灭记忆、已成空白 提交于 2020-05-12 11:32:28

问题


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.

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?


回答1:


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

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.




回答2:


Call setToggleClickCount(0) on the JTree

This will effectively disable expanding on double-click.



来源:https://stackoverflow.com/questions/1223004/overriding-jtree-double-click-to-prevent-node-expansion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!