How to add checkboxes to JTABLE swing [closed]
Asked Answered
C

1

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.

Censurable answered 12/9, 2011 at 17:54 Comment(3)
What have you tried?Representation
the Swing tutorials tell you exactly how to do such a thing. I recommend you have a look.Transvestite
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 :(Ancon
S
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);
            }
        });
    }
}
Sheen answered 12/9, 2011 at 18:23 Comment(4)
-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 thatFluid
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, :-)Sheen
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.Lucey
Your code has been shamelessly copied here. I have flagged the moderators regarding that poster's actions.Transvestite

© 2022 - 2024 — McMap. All rights reserved.