为什么 JTable 中的布尔值显示为 true/false 而不是复选框?
新的布尔值不应该显示为复选框吗?它显示为真/假字符串值。这就是我填充数据的方式:
Object[] columnNames4 = {"Name", "City", "Checkbox"};
Object[][] data = {
{"john smith",
"x", new Boolean(false)},
{"jenny m",
"y", new Boolean(false)}
};
JTable table4 = new JTable(data, columnNames4);
JScrollPane S3 = new JScrollPane(table4);
S3.setPreferredSize(new Dimension(300, 300));
Shouldn't the new Boolean appear as a checkbox? It's showing up as a true/false string value instead. This is how I'm populating the data:
Object[] columnNames4 = {"Name", "City", "Checkbox"};
Object[][] data = {
{"john smith",
"x", new Boolean(false)},
{"jenny m",
"y", new Boolean(false)}
};
JTable table4 = new JTable(data, columnNames4);
JScrollPane S3 = new JScrollPane(table4);
S3.setPreferredSize(new Dimension(300, 300));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建 AbstractTableModel 的子类并返回您想要的列的方法 getColumnClass() 中的 Boolean.class 。
Create a subclass ofAbstractTableModel and return Boolean.class in the Method getColumnClass() for the column you want.
您需要设置 TableCellRenderer 和 TableCellEditor,
请参阅 http://docs.oracle。 com/javase/tutorial/uiswing/components/table.html
一旦定义了渲染器/编辑器,您就可以
将渲染器/编辑器定义为类型的默认渲染器
<代码>
table.setDefaultRenderer(Boolean.class, new MyBooleanRender());
table.setDefaultEditor(Boolean.class, new MyBooleanCellEditor());
将渲染/编辑器定义为列
<代码>
TableColumnModel tcm = table.getColumnModel();
tcm.getColumn(4).setCellRenderer(new MyBooleanRender());
tcm.getColumn(4).setCellEditor(new MyBooleanCellEditor());
您可以定义一个复选框 cellRender,如下所示:
对于 checkboxEditor,您可以使用 DefaultCellEditor 类。
You need to setup a TableCellRenderer and TableCellEditor
see http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
Once you have defined the render's / editor's you can
define the render/editor as the default render for a Type
table.setDefaultRenderer(Boolean.class, new MyBooleanRender());
table.setDefaultEditor(Boolean.class, new MyBooleanCellEditor());
define the render / editor to a column
TableColumnModel tcm = table.getColumnModel();
tcm.getColumn(4).setCellRenderer(new MyBooleanRender());
tcm.getColumn(4).setCellEditor(new MyBooleanCellEditor());
You can define a checkbox cellRender like:
For a checkboxEditor, you can use the DefaultCellEditor class.