Diego Macario
Diego Macario

Reputation: 1230

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...

Upvotes: 1

Views: 1755

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

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.

Upvotes: 2

Related Questions