java 带复选框的 JTree

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1223188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:42:11  来源:igfitidea点击:

JTree with checkboxes

javaswingcheckboxjtree

提问by Jason S

I need to add checkboxes to a JTree. A custom TreeCellRenderer/TreeCellEditor seems like the right approach. So far I used the CheckBoxNodeRenderer approach in this webpage. It works OK except for two things:

我需要将复选框添加到 JTree。自定义 TreeCellRenderer/TreeCellEditor 似乎是正确的方法。到目前为止,我在此网页中使用了 CheckBoxNodeRenderer 方法。除了两件事外,它工作正常:

  1. there's additional whitespace above + below the checkbox; I'd like to keep it the same as a regular JTree.
  2. I would like to distinguish between clicking on the checkbox itself (which should attempt to toggle the checkbox) and clicking on the text associated with the checkbox (which should allow an event listener to interpret this as clicking on the corresponding tree node and take whatever action is appropriate)
  1. 复选框上方+下方有额外的空格;我想保持它与常规 JTree 相同。
  2. 我想区分单击复选框本身(应该尝试切换复选框)和单击与复选框关联的文本(这应该允许事件侦听器将此解释为单击相应的树节点并采取任何操作是合适的)

is there a way to do these things? I looked around for JTrees with checkboxes, can't find much. JIDE looks good but I need to use free open-source software (GPL is not ok, LGPL is ok) in this case. (or create my own checkbox tree)

有没有办法做这些事情?我四处寻找带有复选框的 JTrees,找不到太多。JIDE 看起来不错,但在这种情况下我需要使用免费的开源软件(GPL 不行,LGPL 可以)。(或创建我自己的复选框树)

采纳答案by aperkins

As for #2, you could make a panel be the editor/renderer, and add a label along with the checkbox - the label would be the text, and the check box would not have the text added to it.

至于#2,你可以让一个面板成为编辑器/渲染器,并在复选框中添加一个标签 - 标签将是文本,而复选框不会添加文本。

回答by metator

I know this question has been answered already, but i just want to clear some points:

我知道这个问题已经有人回答了,但我只想澄清几点:

1) JIDE Common Layer is dual-licensed (GPL with classpath exception and free commercial license). This means that you can use the Common Layer Project without any licensing issues. Please check the following link to confirm: http://www.jidesoft.com/products/oss.htm. The Common Layer includes an implementation of a checkable JTree (com.jidesoft.swing.CheckBoxTree).

1) JIDE 公共层是双重许可的(GPL 类路径例外和免费商业许可)。这意味着您可以在没有任何许可问题的情况下使用公共层项目。请查看以下链接进行确认:http: //www.jidesoft.com/products/oss.htm。公共层包括一个可检查的 JTree (com.jidesoft.swing.CheckBoxTree) 的实现。

2) There's a blog dated from 2005 from the Master himself, Santhosh Kumar, where he explains how to implement a JTree with checkboxes with the requirements you mentioned: http://www.jroller.com/santhosh/entry/jtree_with_checkboxes. It's worth reading it, in my opinion.

2) 大师本人 Santhosh Kumar 于 2005 年发表了一篇博客,其中解释了如何使用具有您提到的要求的复选框实现 JTree:http: //www.jroller.com/santhosh/entry/jtree_with_checkboxes。在我看来,值得一读。

回答by Jason S

per @aperkins suggestion this is what I ended up doing in the TableCellRenderer, it seems to work well:

根据@aperkins 的建议,这就是我最终在 TableCellRenderer 中所做的,它似乎运行良好:

final private JPanel nodeRenderer = new JPanel();
final private JLabel label = new JLabel();
final private JCheckBox check = new JCheckBox();

     ...

// in constructor:
final Insets inset0=new Insets(0,0,0,0);        
this.check.setMargin(inset0);
this.nodeRenderer.setLayout(new BorderLayout()); 
this.nodeRenderer.add(this.check, BorderLayout.WEST);
this.nodeRenderer.add(this.label, BorderLayout.CENTER);

The keys for getting rid of unwanted space in the margins seems to be (a) calling JCheckBox.setMargin() to reduce the checkbox margin, and (b) using a BorderLayout for JPanel.

去除边距中不需要的空间的关键似乎是 (a) 调用 JCheckBox.setMargin() 以减少复选框边距,以及 (b) 使用 JPanel 的 BorderLayout。

回答by ctrueden

I have released a standalone Swing Checkbox Treeproject, available from Maven Central as org.scijava:swing-checkbox-tree.

我发布了一个独立的Swing Checkbox Tree项目,可从 Maven Central 作为org.scijava:swing-checkbox-tree.

The package is based on John Zukowski's CheckBox Node Tree Samplecode. It is BSD-2-licensed with no dependencies.

该包基于 John Zukowski 的CheckBox 节点树示例代码。它是 BSD-2 许可的,没有依赖项。

It allows mixing and matching of DefaultMutableTreeNodeand CheckBoxNodeDatanode types. It also allows check box nodes as non-leaf nodes.

它允许混合和匹配DefaultMutableTreeNodeCheckBoxNodeData节点类型。它还允许复选框节点作为非叶节点。

Regarding the question's issue #1: I did not test on all platforms, but on my OS X systems, the CheckBoxNodeDatanodes are exactly the same height in pixels as the DefaultMutableTreeNodenodes.

关于问题的问题 #1:我没有在所有平台上进行测试,但在我的 OS X 系统上,CheckBoxNodeData节点与DefaultMutableTreeNode节点的像素高度完全相同。

Regarding the question's issue #2: it uses a JCheckBox+ JLabelin a JPanel(as aperkinssuggests) to differentiate between clicking on a check box (to check/uncheck a node) versus a label (to select a node).

关于这个问题的问题#2:它采用了JCheckBox+JLabelJPanel(如aperkins建议)点击复选框来区分(检查/取消选中一个节点)与标签(选择节点)。

Example usages:

示例用法:

The latter example also has code for recursively toggling subtrees in response to boxes being checked or unchecked (see the treeNodesChangedmethod).

后一个示例还包含递归切换子树的代码,以响应选中或未选中的框(请参阅treeNodesChanged方法)。