16

Does anyone know how to put a JCheckBox in a JTable column? Something like this:

Table from the table tutorial

I took this from How To use Tables

Thanks in advance.

DLJ
  • 333
  • 2
  • 6
  • 19
java2world
  • 219
  • 1
  • 2
  • 4
  • 5
    [What have you tried?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – Brian Roach Sep 12 '11 at 17:59
  • 4
    the Swing tutorials tell you exactly how to do such a thing. I recommend you have a look. – Hovercraft Full Of Eels Sep 12 '11 at 18:09
  • You can get the "checkboxes" column after table creation, and then `setCellEditor( layersTable.getDefaultEditor( Boolean.class ) )` and cell renderer similar way. PS. I regret this is closed and I can't answer, it's perfect question for quick answer :( – Line Aug 07 '18 at 16:54

1 Answers1

50

1) JTable knows JCheckbox with built-in Boolean TableCellRenderers and TableCellEditor by default, then there is contraproductive declare something about that,

2) AbstractTableModel should be useful, where is in the JTable required to reduce/restrict/change nested and inherits methods by default implemented in the DefaultTableModel,

3) consider using DefaultTableModel, (if you are not sure about how to works) instead of AbstractTableModel,

table_with_BooleanType_column

could be generated from simple code:

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

public class TableCheckBox extends JFrame {

    private static final long serialVersionUID = 1L;
    private JTable table;

    public TableCheckBox() {
        Object[] columnNames = {"Type", "Company", "Shares", "Price", "Boolean"};
        Object[][] data = {
            {"Buy", "IBM", new Integer(1000), new Double(80.50), false},
            {"Sell", "MicroSoft", new Integer(2000), new Double(6.25), true},
            {"Sell", "Apple", new Integer(3000), new Double(7.35), true},
            {"Buy", "Nortel", new Integer(4000), new Double(20.00), false}
        };
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        table = new JTable(model) {

            private static final long serialVersionUID = 1L;

            /*@Override
            public Class getColumnClass(int column) {
            return getValueAt(0, column).getClass();
            }*/
            @Override
            public Class getColumnClass(int column) {
                switch (column) {
                    case 0:
                        return String.class;
                    case 1:
                        return String.class;
                    case 2:
                        return Integer.class;
                    case 3:
                        return Double.class;
                    default:
                        return Boolean.class;
                }
            }
        };
        table.setPreferredScrollableViewportSize(table.getPreferredSize());
        JScrollPane scrollPane = new JScrollPane(table);
        getContentPane().add(scrollPane);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                TableCheckBox frame = new TableCheckBox();
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocation(150, 150);
                frame.setVisible(true);
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 12
    -1 for subclassing the table for data-related reasons: it's clearly the responsibility of the _model_ to return the correct classes, the view _must not_ second guess that – kleopatra Sep 14 '11 at 15:17
  • 3
    1) I never heard about subClassing issues, seriously do not understand, it might be appropriate to clarify the impetus with posting here a bright example, or link to with detailed subscription, 2) maybe there deepest reasin for why I block non-subclassing code just and only about swift level for OP , 3) imaging that, :-) – mKorbel Sep 14 '11 at 15:28
  • 5
    The default `JTable#getColumnClass` method does this: `return getModel().getColumnClass(convertColumnIndexToModel(column));`. If a user moves the table column, this "solution" will break. `TableModel#getColumnClass` is what should be overridden. – Dennis May 11 '13 at 12:57
  • 2
    Your code has been shamelessly copied [here](http://stackoverflow.com/a/25533492/522444). I have flagged the moderators regarding that poster's actions. – Hovercraft Full Of Eels Sep 20 '14 at 15:20