Search code examples
javaswingjtablejbuttonjcheckbox

How can I put a JCheckbox on a JTable?


How can I put a JCheckbox or a JButton on a specific row and column of a JTable?


Solution

  • Not sure about a button, but here is a working example to put a checkbox:

    import javax.swing.*;
    import javax.swing.table.*;
    
    public class Test {
      public static void main(String [] args) throws Exception {
        DefaultTableModel model = new DefaultTableModel(null, new String [] {"CheckMe", "Value"}) {
                                    public Class getColumnClass(int c) {
                                      switch (c) {
                                        case 0: return Boolean.class;
                                        default: return String.class;
                                      }   
                                    } };
        JTable table = new JTable(model);
        JFrame frame = new JFrame("CheckBox Test");
        frame.add(table);
        model.addRow(new Object [] {true, "This is true"});
        model.addRow(new Object [] {false, "This is false"});
        frame.pack(); frame.validate();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
      }
    }