如何将 JCheckbox 放在 JTable 上?

发布于 2024-10-30 21:32:46 字数 88 浏览 6 评论 0原文

如何将 JCheckboxJButton 放在 JTable 的特定行和列上?

How can I put a JCheckbox or a JButton on a specific row and column of a JTable?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

云巢 2024-11-06 21:32:46

不确定按钮,但这是一个放置复选框的工作示例:

import javax.swing.*;
import javax.swing.table.*;

public class Test {
  public static void main(String [] args) throws Exception {
    DefaultTableModel model = new DefaultTableModel(null, new String [] {"CheckMe", "Value"}) {
                                public Class getColumnClass(int c) {
                                  switch (c) {
                                    case 0: return Boolean.class;
                                    default: return String.class;
                                  }   
                                } };
    JTable table = new JTable(model);
    JFrame frame = new JFrame("CheckBox Test");
    frame.add(table);
    model.addRow(new Object [] {true, "This is true"});
    model.addRow(new Object [] {false, "This is false"});
    frame.pack(); frame.validate();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}

Not sure about a button, but here is a working example to put a checkbox:

import javax.swing.*;
import javax.swing.table.*;

public class Test {
  public static void main(String [] args) throws Exception {
    DefaultTableModel model = new DefaultTableModel(null, new String [] {"CheckMe", "Value"}) {
                                public Class getColumnClass(int c) {
                                  switch (c) {
                                    case 0: return Boolean.class;
                                    default: return String.class;
                                  }   
                                } };
    JTable table = new JTable(model);
    JFrame frame = new JFrame("CheckBox Test");
    frame.add(table);
    model.addRow(new Object [] {true, "This is true"});
    model.addRow(new Object [] {false, "This is false"});
    frame.pack(); frame.validate();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}
分開簡單 2024-11-06 21:32:46

从 khachik 的答案中可以看出,对复选框的支持是由基于列的列类的表提供的。

但是,如果您只需要特定列的特定行上的复选框,则需要重写 getCellRenderer(...) 和 getCellEditor(...) 方法以返回给定单元格的渲染器/编辑器。像这样的东西:

public TableCellEditor getCellEditor(int row, int column)
{
    int modelColumn = convertColumnIndexToModel( column );

    if (modelColumn == 1 && row < 3)
        return getDefaultEditor(Boolean.class);
    else
        return super.getCellEditor(row, column);
}

As you can tell from khachik's answer support for a check box is provided by a table based on the column class of the column.

However, if you only want a check box on a specific row of a specific column then you need to override the getCellRenderer(...) and getCellEditor(...) methods to return the renderer/editor for the given cell. Something like:

public TableCellEditor getCellEditor(int row, int column)
{
    int modelColumn = convertColumnIndexToModel( column );

    if (modelColumn == 1 && row < 3)
        return getDefaultEditor(Boolean.class);
    else
        return super.getCellEditor(row, column);
}
一片旧的回忆 2024-11-06 21:32:46

为此,您必须编写一个 TableCellRenderer 和一个 TableCellEditor

您可以从默认的 swing 实现中派生以使其更容易。

在每个类中,您必须重写这些接口的一个方法,并在其中检查传递的 rowcolumn 参数;如果行和列都符合您的条件,则返回 JCheckBoxJButton,否则返回 super< 返回的 JComponent /code> 实现(当使用这些接口的默认 swing 实现时)。

For that, you'll have to write a TableCellRenderer and a TableCellEditor.

You can derive from default swing implementations to make it easier.

In each class, you'll have to override the one method of these interfaces, and in it, check the passed row and column arguments; if both row and column match your criteria, then return a JCheckBox or a JButton, otherwise return the JComponent returned by the super implementation (when using default swing implementations of these interfaces).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文