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

Checkboxes in a JTabel column instead of boolean values using CellRenderer

我正在向我的其中一个专栏添加单元格渲染器。这是为了 return 复选框而不是布尔值。通过执行以下操作,我可以获得传递布尔值的复选框,但我无法 check/uncheck 复选框。

如果我重写 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;
    }
}
}

现在,您可以练习实现自己的渲染器和编辑器,或者让 table 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;
}

您的 dtmInsurance 实施并获得

免费。

否则你应该看看 Concepts: Editors and Renderers and Using Other Editors 了解更多关于自己制作的细节:P

自定义编辑器可能类似于...

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;
    }
}

您可以使用类似于...

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;
        }
    }
}

But if I have 10 rows in my table, the checkbox which i selected first as soon as starting the program, only for that checkbox I can able to check/uncheck. Not for all

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

更像是...

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;
    }
}

似乎有效