为什么我的 JTable CellRenderer 一直在运行?

发布于 2024-10-09 11:06:32 字数 655 浏览 3 评论 0 原文

//新手问题

我有一个带有几乎基本单元格渲染器的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;

  }

//newbie question

I have a JTable with an almost basic cell renderer (it colors the line differently).
I've noticed my cell renderer is constantly running for the lines that are displayed on the screen, even when I don't do anything with the table.

Is that how it's supposed to be? Shouldn't it have rendered each cell once , and that's it?
How can I make it stop , and only recalculate on change?

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 技术交流群。

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

发布评论

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

评论(4

我们只是彼此的过ke 2024-10-16 11:06:32

按照 @Steve McLeod 的提议,我开始构建一个具有相同错误的示例代码,直到那时我才意识到,在我的一个 CellRenderers 中,

@Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {

        log.debug("Building a list for " + row + "," + column);
        setListData(((Vector<String>)value).toArray());     
        setToolTipText("This is a tool tip for " + row + "," + column);

        table.setRowHeight(row, Math.max(1, getPreferredSize().height));
        Filter filter = (Filter)table.getModel().getValueAt(row, Column.CLASSIFICATION_RESULT.getIndex());
        setBackground(filter.getColor());       
        return this;
    }

我有一行 :

table.setRowHeight(row, Math.max(1, getPreferredSize().height));

,并且它不断地更改行行,而同一行中的其他渲染器则这样做相同...
所以他们不断地互相射击。

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

@Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {

        log.debug("Building a list for " + row + "," + column);
        setListData(((Vector<String>)value).toArray());     
        setToolTipText("This is a tool tip for " + row + "," + column);

        table.setRowHeight(row, Math.max(1, getPreferredSize().height));
        Filter filter = (Filter)table.getModel().getValueAt(row, Column.CLASSIFICATION_RESULT.getIndex());
        setBackground(filter.getColor());       
        return this;
    }

I had the line :

table.setRowHeight(row, Math.max(1, getPreferredSize().height));

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.

罗罗贝儿 2024-10-16 11:06:32

我使用过很多包含大量数据的 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).

日久见人心 2024-10-16 11:06:32

我注意到您根据另一个单元格的值更改了一个单元格的外观。如果这种关系是双向的 - 单元格 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.

总攻大人 2024-10-16 11:06:32

渲染器已经缓存了渲染的组件,当没有任何更改时,不会重新渲染任何内容。

但是,当表检测到某些内容可能已更改时,它将请求重新渲染。
最触发它的事件是鼠标移动。

所以是的,这是 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.

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