如何使 JTable 列包含复选框?
前言:我对 java 很糟糕,对 java ui 组件更糟糕。
我找到了几个关于如何向表格添加按钮的不同教程,但是我在添加复选框方面遇到了困难。我需要有一列在默认情况下绘制一个选中的文本框(我认为单元格渲染器可以处理该文本框),然后单击复选框,取消选中该框,重新绘制所述框,并在我可以跟踪的地方触发一个事件。
目前我有一个自定义单元渲染器:
public class GraphButtonCellRenderer extends JCheckBox implements TableCellRenderer {
public GraphButtonCellRenderer() {
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if(isSelected)
setSelected(true);
else
setSelected(false);
setMargin(new Insets(0, 16, 0, 0));
setIconTextGap(0);
setBackground(new Color(255,255,255,0));
return this;
}}
当前处理绘制复选框,但仅在选择该行时勾选和取消勾选该框。但我不知道如何处理这些事件。我真正要问的是可能是一个关于如何将复选框干净地添加到 JTable 的好教程的链接。 非常感谢任何帮助:)
Preface: I am horrible with java, and worse with java ui components.
I have found several different tutorials on how to add buttons to tables, however I am struggling with adding checkboxes. I need to have a column that draws a text box ticked on default (cell renderer i think handles that), then on click of tickbox, unticks the box, redraws said box, and fires off an event somewhere I can track.
currently I have a custom cellrenderer:
public class GraphButtonCellRenderer extends JCheckBox implements TableCellRenderer {
public GraphButtonCellRenderer() {
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if(isSelected)
setSelected(true);
else
setSelected(false);
setMargin(new Insets(0, 16, 0, 0));
setIconTextGap(0);
setBackground(new Color(255,255,255,0));
return this;
}}
Which currently handles drawing the tick box, but only ticks and unticks the box if that row is selected. But I don't know how to handle the events. Really what I am asking is possibly a link to a good tutorial on how to add checkboxes cleanly to a JTable.
Any assist is greatly appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
无需创建您自己的表格渲染器。 这是一个更简单的示例。只需创建一个自定义表模型,并为给定的列返回 Boolean 类:
如果您希望该列可编辑,则返回 true,
JTable 会为您处理渲染。
另一个示例在这里。
There's no need to create your own table renderer. Here's a simpler example. Just create a custom table model and for a given column return the class Boolean for:
If you want the column to be editable, return true for
JTable takes care of the rendering for you.
Another example is here.
正如 Peter 所说,使用扩展的 DefaultTableModel 类很容易,例如:
As Peter say, its easy using extended DefaultTableModel class, ex:
这是一个使用
的
和简单相当复杂的示例 TableCellRendererTableCellEditor
。另请参阅概念:编辑器和渲染器< /em>。附录:@Jay Askren 的观点很好理解。正如教程中所述,
Boolean.class
的默认渲染器可能就是您所需要的。Here's a
simplerather elaborate example using aTableCellRenderer
andTableCellEditor
. See also, Concepts: Editors and Renderers.Addendum: @Jay Askren's point is well taken. The default renderer for
Boolean.class
, as described in the tutorial, may be all you need.最简单的解决方案是使用 DefaultTableModel 并使用 Boolean 对象作为值。
The easiest solution is to use the DefaultTableModel and use Boolean object as values.
在 Swing Designer 中将列类型设置为布尔值
In the Swing Designer set column type to boolean