如何在JTable中添加一个空行? [英] How do I add an empty row to JTable?

查看:376
本文介绍了如何在JTable中添加一个空行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以向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?

我需要它来分隔行组. 我尝试使用下面的代码,但不起作用:

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

您需要存储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.

这篇关于如何在JTable中添加一个空行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆