如何从 CustomCellRenderer 为特定 JTable 单元调用 DefaulCellleRenderer
我有一个类,其中有一个布尔字段。我在 JTable 中显示这些类的列表。 我创建了一个 CustomCellRenderer 来更改行的背景颜色,这样我就可以有不同的颜色。
问题:当自定义渲染器应用于布尔字段时,会渲染 (true/false) 而不是默认渲染器的复选框。
我怎样才能同时拥有这两个功能:背景颜色和复选框?
这是customrenderer代码:
public class CustomCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object obj,boolean isSelected, boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(table, obj,isSelected, hasFocus, row, column);
if (isSelected) {
cell.setBackground(Color.red);
} else {
if (row % 2 == 0) {
cell.setBackground(new Color(110,134,214));
} else {
cell.setBackground(Color.lightGray);
}
}
return cell;
}
}
提前感谢您的帮助。
I have a class that has a Boolean field in it. I display in a JTable a list of of those classes.
I created a CustomCellRenderer to change the background color of the rows, so I could have different colors.
the problem: when the customrenderer is applied on the Boolean field, (true/false) is rendered instead of the default renderer's checkbox.
how can I have both features: background colors and checkbox?
here is the customrenderer code:
public class CustomCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object obj,boolean isSelected, boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(table, obj,isSelected, hasFocus, row, column);
if (isSelected) {
cell.setBackground(Color.red);
} else {
if (row % 2 == 0) {
cell.setBackground(new Color(110,134,214));
} else {
cell.setBackground(Color.lightGray);
}
}
return cell;
}
}
thanks in advance for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我得到了它。
我发现了这个:
仅向 JTable 中的一个单元格添加一个复选框
我使用了从 CustomCellRenderer 的 getTableCellRendererComponent 方法调用的 BooleanRenderer。
我刚刚在这个方法中添加了一行委托渲染:
I got it.
I found this :
add a check box to only one cell in a JTable
I used the BooleanRenderer that I called from the getTableCellRendererComponent method of the CustomCellRenderer.
I just added a line in this method the delegate the rendering: