为什么我的 JTable CellRenderer 一直在运行?
//新手问题
我有一个带有几乎基本单元格渲染器的JTable(它以不同的方式为线条着色)。 我注意到我的单元格渲染器不断运行以显示屏幕上的行,即使我没有对表格执行任何操作。
事情就应该这样吗?难道它不应该将每个单元格渲染一次,仅此而已吗? 我怎样才能让它停止,并且只在变化时重新计算?
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
log.debug("Building cell : " + row + "," + column);
Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
Filter filter = (Filter)table.getModel().getValueAt(row, Column.CLASSIFICATION.getIndex());
comp.setBackground(filter.getColor());
return comp;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
按照 @Steve McLeod 的提议,我开始构建一个具有相同错误的示例代码,直到那时我才意识到,在我的一个 CellRenderers 中,
我有一行 :
,并且它不断地更改行行,而同一行中的其他渲染器则这样做相同...
所以他们不断地互相射击。
Following @Steve McLeod's offer , I started building an example code with the same bug , and only then I realized that inside one of my CellRenderers
I had the line :
and it constantly changed the row line , while the other renderers in the same line did the same...
so they were constantly firing each other.
我使用过很多包含大量数据的 JTable。超过 99.9% 的 Java 程序员通常操作的内容。您需要知道一件事:默认情况下会产生大量浪费,并完成大量通常不必要的操作。
如果您追求快速高效的 JTable,那么 Sun 的有关该主题的权威文章:
“圣诞树应用程序,如何创建性能良好的频繁更新的 JTable”
请注意标题中的“表现良好”,因为默认情况下 JTable 性能确实非常糟糕:
原始链接 (Sun)
当前链接 (Oracle)
存档版本:
实施该文章中建议的两到三种技术后,您会注意到 JTable 渲染的速度有了惊人的提高,并且您会注意到生成的垃圾要少得多(并且因此 GC 需要更少地启动)。
I've worked a lot with JTables containing a lot of data. More than what 99.9% of Java programmers typically manipulate. There's one thing you need to know: by default there's an insane amount of waste generated and an insane amount of typically needless operation done.
If you're after fast and efficient JTables, then there's the authoritative Sun article on the subject:
"Christmas Tree Applications, How to Create Frequently-Updated JTables that Perform Well"
Notice the "that Perform Well" in the title, because by default JTable perfs are really, really pathetically bad:
Original Link (Sun)
Current link (Oracle)
Archived Version:
After implementing two or three of the techniques adviced in that article, you'll notice amazing speedup in your JTable rendering and you'll notice that much less garbage is generated (and hence the GC needs to kick in way less often).
我注意到您根据另一个单元格的值更改了一个单元格的外观。如果这种关系是双向的 - 单元格 1 的外观基于单元格 2,反之亦然,您可能会遇到这样的问题。但实际上,我们需要一个独立的代码示例来重现问题 - 否则我们只能在黑暗中拍摄。
I notice you change the appearance of a cell based on the value of another cell. If this relationship goes both ways - cell 1 appearance is based on cell 2 and vice versa you could get such a problem. But really, we need a self-contained code example that reproduces the problem - otherwise we can only shoot in the dark.
渲染器已经缓存了渲染的组件,当没有任何更改时,不会重新渲染任何内容。
但是,当表检测到某些内容可能已更改时,它将请求重新渲染。
最触发它的事件是鼠标移动。
所以是的,这是 JTable 的正常行为。
the renderer already caches your rendered component, when nothing changes nothing gets rerendered
However when the table detects something MIGHT have changed it will request a re-rendering.
The event that triggers it the most is a mouse move.
So yes its normal behaviour for a JTable.