Java 如何使 JTable 列包含复选框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2901470/
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-08-13 14:18:55  来源:igfitidea点击:

How to make JTable column contain checkboxes?

javaswingjtablejcheckboxtablecellrenderer

提问by theraven

Preface: I am horrible with java, and worse with java ui components.

前言:我对 java 很糟糕,对 java ui 组件更糟。

I have found several different tutorials on how to add buttons to tables, however I am struggling with adding checkboxes. I need to have a column that draws a text box ticked on default (cell renderer i think handles that), then on click of tickbox, unticks the box, redraws said box, and fires off an event somewhere I can track.

我找到了几个关于如何向表格添加按钮的不同教程,但是我正在努力添加复选框。我需要有一列绘制一个默认勾选的文本框(我认为单元格渲染器可以处理),然后点击勾选框,取消勾选框,重绘所述框,并在我可以跟踪的地方触发一个事件。

currently I have a custom cellrenderer:

目前我有一个自定义的 cellrenderer:

public class GraphButtonCellRenderer extends JCheckBox implements TableCellRenderer {
public GraphButtonCellRenderer() {
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
    if(isSelected)
        setSelected(true);
    else
        setSelected(false);
    setMargin(new Insets(0, 16, 0, 0));
    setIconTextGap(0);

    setBackground(new Color(255,255,255,0));
    return this;
}}

Which currently handles drawing the tick box, but only ticks and unticks the box if that row is selected. But I don't know how to handle the events. Really what I am asking is possibly a link to a good tutorial on how to add checkboxes cleanly to a JTable. Any assist is greatly appreciated :)

当前处理绘制勾选框,但如果该行被选中,则仅勾选和取消勾选该框。但我不知道如何处理这些事件。我真正要问的可能是一个关于如何将复选框干净地添加到 JTable 的好教程的链接。非常感谢任何帮助:)

采纳答案by Jay Askren

There's no need to create your own table renderer. Here's a simpler example. Just create a custom table model and for a given column return the class Boolean for:

无需创建自己的表格渲染器。 这是一个更简单的例子。只需创建一个自定义表模型,并为给定列返回 Boolean 类:

public Class getColumnClass(int column)

If you want the column to be editable, return true for

如果您希望该列可编辑,则返回 true

public boolean isCellEditable(int row, int column)

JTable takes care of the rendering for you.

JTable 会为您处理渲染。

Another example is here.

另一个例子在这里。

回答by trashgod

Here's a simplerather elaborate exampleusing a TableCellRendererand TableCellEditor. See also, Concepts: Editors and Renderers.

这是一个使用 a和的简单而详尽的示例。另请参阅概念:编辑器和渲染器TableCellRendererTableCellEditor

Addendum: @Jay Askren's point is well taken. The default renderer for Boolean.class, as described in the tutorial, may be all you need.

附录:@Jay Askren 的观点很好。如Boolean.class教程中所述, 的默认渲染器可能就是您所需要的。

回答by Peter

The easiest solution is to use the DefaultTableModel and use Boolean object as values.

最简单的解决方案是使用 DefaultTableModel 并使用布尔对象作为值。

回答by Kachwahed

As Peter say, its easy using extended DefaultTableModel class, ex:

正如彼得所说,使用扩展的 DefaultTableModel 类很容易,例如:

class NewTableModel extends DefaultTableModel{
        public Class<?> getColumnClass(int columnIndex) {
            return getValueAt(0, columnIndex).getClass();
        }
    }

回答by insufferableKnowItAll

In the Swing Designer set column type to boolean

在 Swing 设计器中将列类型设置为布尔值