How do I add an empty row to JTable?

谁都会走 提交于 2019-12-06 09:05:30

问题


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});

回答1:


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.




回答2:


Try

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



回答3:


For the TableColumn instance for your first column, make a cell renderer (by implementing javax.swing.table.TableCellRenderer) that renders a checkbox (javax.swing.JCheckBox) for Boolean values different from null, and renders an empty label (javax.swing.JLabel) for null values. You can't do it with providing a DefaultTableCellRenderer (javax.swing.table.DefaultTableCellRenderer) AFAICT.

Then set the table cell renderer property of the TableColumn instance using TableColumn.setCellRenderer



来源:https://stackoverflow.com/questions/16970675/how-do-i-add-an-empty-row-to-jtable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!