简体   繁体   English

如何在JTable中添加一个空行?

[英]How do I add an empty row to JTable?

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? 有什么办法可以向jtable添加一个空行,其中第一列是布尔值,因此它不会显示自动生成的复选框?

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

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. 无需在模型中存储Boolean.TRUE或Boolean.FALSE,您需要存储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. 重写JTable.getCellRenderer()方法。 to return the renderer for the Object.class when the value is null. 当该值为null时返回Object.class的呈现器。 The default renderer for the Oject class will display null as an empty string. Oject类的默认渲染器将显示null为空字符串。

尝试

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

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. 对于第一列的TableColumn实例,创建一个单元格渲染器(通过实现javax.swing.table.TableCellRenderer),以渲染一个复选框(javax.swing.JCheckBox)以获取不同于null的布尔值,并渲染一个空标签(javax)。 swing.JLabel)为空值。 You can't do it with providing a DefaultTableCellRenderer (javax.swing.table.DefaultTableCellRenderer) AFAICT. 您不能通过提供DefaultTableCellRenderer(javax.swing.table.DefaultTableCellRenderer)AFAICT来做到这一点。

Then set the table cell renderer property of the TableColumn instance using TableColumn.setCellRenderer 然后使用TableColumn.setCellRenderer设置TableColumn实例的表单元格渲染器属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM