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

时间:2015-06-30 07:12:26

标签: java swing jtable jcheckbox tablecellrenderer

我正在为我的一个列添加一个单元格渲染器。这是为了返回复选框而不是布尔值。通过执行以下操作,我可以获取传递布尔值的复选框,但我无法选中/取消选中框。

如果我覆盖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;
    }
}
}

1 个答案:

答案 0 :(得分:2)

现在,您可以通过练习实现自己的渲染器和编辑器,或者您可以让表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实施并获取

NotSelected Selected

免费。

否则,您应该查看Concepts: Editors and RenderersUsing 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;
        }
    }
}
  

但是如果我的表中有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;
    }
}

似乎工作