How do I create checkboxes in JTables?

George Tzallas

Assuming that you have a model for your JTable then what you need to do is implement

public Class getColumnClass (int column)

as follows

public Class getColumnClass (int column)
{
    if ( column == 3 )
        return Boolean.class    ;

    return super.getColumnClass (column);
    // Or you could also almost safely use return String.class
}

By returning a Boolean.class, then your cell will be rendered using a checkbox, whereas Strings are rendered using a JLabel.

Note that this applies only for the example you have given, ie, you want the 4th column to be a check box.

If you retrieve data from another table, then the 4th column will be a checkbox, and that may not be what you want.

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact