Java JTable——当外部变量更改时突出显示某个条目

发布于 2024-10-09 12:09:34 字数 583 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

驱逐舰岛风号 2024-10-16 12:09:34

它仅突出显示特定的(行、列)条目。我想突出显示整行。如何突出显示同一行中的其他列?

我喜欢为此使用表格行渲染

当变量:HighlightEntryID 更改时,JTable 不会自动反映更改。我必须手动刷新表格。如何自动刷新?

调用以下命令强制重新绘制整个表:

table.repaint();

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?

I like to use Table Row Rendering for this.

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?

Invoke the following to force repainting of the entire table:

table.repaint();
凯凯我们等你回来 2024-10-16 12:09:34

您可以使用 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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文