Search code examples
javaswingjtablejcheckboxdefaulttablemodel

How to add a checkbox in a column in a table in Java


i´m trying to put a checkbox in a column in my table...but i shows de boolean value, when i click over the cell, it shows the checkbox and sooner show the boolean value...

    public class Tabela {

    private JTable tabela;
    private JCheckBox checkbox;

    public Tabela(Object[][] linhas, String[] nomeColunas) {    
        this.tabela = new JTable();
        this.tabela.setModel(new DefaultTableModel(linhas, nomeColunas));
        this.checkbox = new JCheckBox();
        this.tabela.setFillsViewportHeight(true);
        this.tabela.getColumn("Selecione").setCellEditor(
              new DefaultCellEditor(checkbox));    
    }

    public JTable getTabela() {    
        return tabela;    
    }    
}

So what´s wrong with my code?

I create seppareted my form, table and panel...


Solution

  • You must override getColumnClass(...) and return Boolean.class for the column that should display the checkboxes. The data model will need to hold Boolean objects for that column as well.