How do I add an empty row to JTable?

Michal picture Michal · Jun 6, 2013 · Viewed 8k times · Source

is there any way to an add empty row to jtable, where the first column is boolean, so it won't display the auto-generated checkbox?

I need it to separate groups of rows. I tried using the code below, but it is NOT working:

model.addRow(new Object[]{null,null,null,null});

Answer

camickr picture camickr · Jun 6, 2013

I want make some rows completely empty so they work as separators. But in rest I would like to keep checkboxes

Instead of storing Boolean.TRUE or Boolean.FALSE in the model you would need to store null.

Then you have two options:

  1. Create a custom renderer that displays the Boolean values normally and recognizes the null value and doesn't display anything.

  2. Override the JTable.getCellRenderer() method. to return the renderer for the Object.class when the value is null. The default renderer for the Oject class will display null as an empty string.