Java JTable——当外部变量更改时突出显示某个条目
我有一个 JTable,这样:
ID #: Name:
0 Entry 0
1 Entry 1
2 Entry 2
当变量:
private int HighlightEntryID = 2;
设置为 ID # 时,JTable 应该能够突出显示相应的条目。
我创建了一个自定义单元格渲染器并使用以下代码:
TableColumn column = CISTable.getColumnModel().getColumn(0);
column.setCellRenderer(new CustomCellRenderer());
我根据 ID # 列渲染单元格。我的问题是:
- 它只突出显示特定的(行、列)条目。我想突出显示整行。如何突出显示同一行中的其他列?
- 当变量:HighlightEntryID 更改时,JTable 不会自动反映更改。我必须手动刷新表格。如何自动刷新?
I have a JTable such that:
ID #: Name:
0 Entry 0
1 Entry 1
2 Entry 2
When a variable:
private int HighlightEntryID = 2;
is set to an ID #, the JTable should then be able to highlight the corresponding entry.
I have created a Custom Cell Renderer and with the following code:
TableColumn column = CISTable.getColumnModel().getColumn(0);
column.setCellRenderer(new CustomCellRenderer());
I render cells based on the ID # column. My questions are:
- It only highlights a specific (row, column) entry. I want to highlight the whole row. How do I highlight other columns in the same row?
- When the variable: HighlightEntryID is changed, the JTable doesn't automatically reflect the change. I must refresh the table manually. How do I refresh it automatically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我喜欢为此使用表格行渲染。
调用以下命令强制重新绘制整个表:
I like to use Table Row Rendering for this.
Invoke the following to force repainting of the entire table:
您可以使用 JTable 的 setDefaultCellRenderer(Object.class, new CustomCellRenderer()) 方法。
而且你还应该将highlightEntryID变量放入你的表类中。
You can use setDefaultCellRenderer(Object.class, new CustomCellRenderer()) method of JTable..
And also you should put highlightEntryID variable to your table class..