使用单元格渲染器后禁用行选择功能
我显然做错了什么。我的猜测是组件,但这就是问题所在。 这是我的单元格渲染器:
public class WildcardCellRenderer implements TableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
JTextField c = new JTextField();
c.setBorder(javax.swing.BorderFactory.createEmptyBorder());
if(vColIndex == 2){
if((Integer)value == 0) c.setText("No") ;
else c.setText("Si");
} else c.setText(value.toString());
return c;
}
}
这就是我在面板中设置内容的方式:
this.table.setDefaultRenderer(Object.class, new WildcardCellRenderer() );
问题是,当我设置此渲染器时,行变得不可选择。我应该使用不同的组件来显示我需要显示的内容吗?基本上,我将 0/1 值显示为“否”/“是”值。
I'm cleary doing something wrong. My guess would be the component, but here's the problem.
This is my cell renderer:
public class WildcardCellRenderer implements TableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex) {
JTextField c = new JTextField();
c.setBorder(javax.swing.BorderFactory.createEmptyBorder());
if(vColIndex == 2){
if((Integer)value == 0) c.setText("No") ;
else c.setText("Si");
} else c.setText(value.toString());
return c;
}
}
and this is how I set up the thing in my panel:
this.table.setDefaultRenderer(Object.class, new WildcardCellRenderer() );
The problem is that when I set up this renderer the rows become unselectable. Should I use a different Component to display what I need to display? Basically I'm displaying a 0/1 value as a No/Yes value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要考虑“isSelected”值并相应地设置背景颜色,如下所示:
我从我的一个 ListCellRenderers 中获取了它,但我认为它与 TableCellRenderers 的工作方式相同。
哦,顺便说一句,您可以重用文本字段,无需为每个单元格创建一个新的文本字段,因此您可以使文本字段成为您的类的成员,并且在方法内部只需更改文本字段中的某些内容,例如内容/颜色并返回。这样,您就不会在大表上出现一百万个文本字段实例。
You need to take the "isSelected" value into account and set up the background color accordingly like this:
I took this from one of my ListCellRenderers but i assume it works the same with the TableCellRenderers.
Oh and BTW, you can reuse the text field, there is no need to create a new textfield for each cell, so you could make the text field a member of your class and inside the method just change something in your text field like content/color and return it. That way you don't end up with a million textfield instances on a large table.