且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-03 17:27:46

我希望使某些行完全为空,以便它们用作分隔符.但是在休息状态下,我想保留复选框

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

您需要存储null,而不是在模型中存储Boolean.TRUE或Boolean.FALSE.

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

然后您有两个选择:

  1. 创建一个自定义渲染器,该渲染器可正常显示布尔值并识别空值,并且不显示任何内容.

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

重写JTable.getCellRenderer()方法.当该值为null时返回Object.class的呈现器. Oject类的默认渲染器将显示null作为空字符串.

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.