Java JTabel列中的复选框,而不是使用CellRenderer的布尔值

Java JTabel列中的复选框,而不是使用CellRenderer的布尔值,java,swing,jtable,jcheckbox,tablecellrenderer,Java,Swing,Jtable,Jcheckbox,Tablecellrenderer,我正在将单元格渲染器添加到我的一个列中。用于返回复选框而不是布尔值。通过做下面的事情,我可以得到传递布尔值的复选框,但我无法选中/取消选中这些复选框 如果我重写DataTableModel的getColumnClass(),它可以正常工作 但我需要渲染器 public class CustomRenderer { Table table = new JTable(); public DefaultTableModel getDtmInsurance() { if (dtmInsuranc

我正在将单元格渲染器添加到我的一个列中。用于返回复选框而不是布尔值。通过做下面的事情,我可以得到传递布尔值的复选框,但我无法选中/取消选中这些复选框

如果我重写DataTableModel的getColumnClass(),它可以正常工作

但我需要渲染器

public class CustomRenderer
{
Table table = new JTable();
public DefaultTableModel getDtmInsurance()
{
    if (dtmInsurance == null)
    {
        String[] columns = { "LIC ID", "Delete" };
        dtmInsurance = new DefaultTableModel(columns, 0)
        {
            private static final long   serialVersionUID    = 1L;

            @Override
            public boolean isCellEditable(int row, int column)
            {
                if (column == 1)
                    return true;
                return false;
            }
        };
        dtmInsurance.setColumnIdentifiers(columns);
        table.setModel(dtmInsurance);
        Object[] addInsurance = { "0", false };
        dtmInsurance.addRow(addInsurance);
    }
    table.getColumnModel().getColumn(1).setCellRenderer(new MyRenderer());

    return dtmInsurance;
}

class MyRenderer extends DefaultTableCellRenderer
{
    private static final long   serialVersionUID    = 1L;

    JCheckBox                   check               = new JCheckBox();

    public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column)
    {
        Component cell = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, column);
        if (obj instanceof Boolean)
        {
            return check;
        }
        return cell;
    }
}
}

现在,您可以完成实现自己的渲染器和编辑器的练习,也可以让表API来完成

你可以加上

@Override
public Class<?> getColumnClass(int columnIndex) {
    Class type = String.class;
    switch (columnIndex) {
        case 0:
            type = Integer.class;
            break;
        case 1:
            type = Boolean.class;
            break;
    }
    return type;
}
您可以使用类似于

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import javax.swing.AbstractCellEditor;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;

public class CustomRenderer extends JPanel {

    private JTable table = new JTable();
    private DefaultTableModel dtmInsurance;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new CustomRenderer());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public CustomRenderer() {
        setLayout(new BorderLayout());
        table.setModel(getDtmInsurance());
        TableColumn column = table.getColumnModel().getColumn(1);
        column.setCellEditor(new MyBooleanEditor());
        column.setCellRenderer(new MyBooleanRenderer());
        add(new JScrollPane(table));
    }

    public DefaultTableModel getDtmInsurance() {
        if (dtmInsurance == null) {
            String[] columns = {"LIC ID", "Delete"};
            dtmInsurance = new DefaultTableModel(columns, 0) {
                private static final long serialVersionUID = 1L;

                @Override
                public boolean isCellEditable(int row, int column) {
                    if (column == 1) {
                        return true;
                    }
                    return false;
                }
            };
            dtmInsurance.setColumnIdentifiers(columns);
            table.setModel(dtmInsurance);
            Object[] addInsurance = {"0", false};
            dtmInsurance.addRow(addInsurance);
        }

        return dtmInsurance;
    }

    class MyBooleanRenderer extends DefaultTableCellRenderer {

        private static final long serialVersionUID = 1L;

        JCheckBox check = new JCheckBox();

        @Override
        public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
            Component cell = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, column);
            if (obj instanceof Boolean) {
                return check;
            }
            return cell;
        }
    }

    public class MyBooleanEditor extends AbstractCellEditor implements TableCellEditor {

        private JCheckBox check = new JCheckBox();

        @Override
        public Object getCellEditorValue() {
            return check.isSelected();
        }

        @Override
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
            if (value instanceof Boolean) {
                check.setSelected((Boolean)value);
            }
            return check;
        }
    }
}
但是,如果我的表中有10行,那么我在启动程序时首先选中的复选框,只有该复选框我才能选中/取消选中。当然不是

单元格渲染器不会在每次调用复选框时更新其状态,它只是返回一个空复选框

更像是

class MyBooleanRenderer implements TableCellRenderer {

    private static final long serialVersionUID = 1L;

    JCheckBox check = new JCheckBox();

    @Override
    public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
        check.setSelected(false);
        if (obj instanceof Boolean) {
            check.setSelected((Boolean)obj);
        }
        if (isSelected) {
            check.setForeground(table.getSelectionForeground());
            check.setBackground(table.getSelectionBackground());
        } else {
            check.setForeground(table.getForeground());
            check.setBackground(table.getBackground());
        }
        return check;
    }
}

似乎有效

您还需要提供自定义单元格编辑器,有关详细信息,请参阅和。话虽如此,在没有自定义渲染器/editoryeah的情况下,使用上述方法,这应该是可行的。使用上述方法,一切都很好。但是你能用同样的例子吗renderers@SaikiranGosikonda不,因为它不是这样工作的,渲染器“渲染”,编辑器“编辑”,它们是两个不同的概念。您应该首先阅读我链接到注释和答案中的文档,以获得更深入的理解,即使我无法选中/取消选中项目I am override isCellEditable()以返回true。然后我可以选择复选框。但是,如果我的表中有10行,那么我在启动程序时首先选中的复选框,只有该复选框我才能选中/取消选中。并非如此,那么单元格渲染有问题,例如,您没有更改
JCheckBox
的选定状态。。。
class MyBooleanRenderer implements TableCellRenderer {

    private static final long serialVersionUID = 1L;

    JCheckBox check = new JCheckBox();

    @Override
    public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
        check.setSelected(false);
        if (obj instanceof Boolean) {
            check.setSelected((Boolean)obj);
        }
        if (isSelected) {
            check.setForeground(table.getSelectionForeground());
            check.setBackground(table.getSelectionBackground());
        } else {
            check.setForeground(table.getForeground());
            check.setBackground(table.getBackground());
        }
        return check;
    }
}