How to add checkboxes to JTABLE swing

109,195

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);
            }
        });
    }
}
Share:
109,195
Admin
Author by

Admin

Updated on October 17, 2020

Comments

  • Admin
    Admin over 3 years

    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.

  • kleopatra
    kleopatra almost 13 years
    -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
  • mKorbel
    mKorbel almost 13 years
    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, :-)
  • Dennis
    Dennis about 11 years
    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.
  • Hovercraft Full Of Eels
    Hovercraft Full Of Eels almost 10 years
    Your code has been shamelessly copied here. I have flagged the moderators regarding that poster's actions.