自定义 Swing 组件:repaint() 问题

发布于 2024-10-14 04:24:08 字数 773 浏览 5 评论 0原文

我正在构建一个自定义摆动组件(更多的是作为一个实际的思想实验而不是其他任何东西)。

我在重新喷漆时遇到问题。我的组件有一个jtree、一个jtable,然后是完全自定义绘制的部分(希望最终成为 MS 项目式的甘特视图)

具体问题是当 jtree 扩展时,表和 gannt 视图应该更新以反映新扩展的树节点下的新子项目。他们这样做了,但尺寸没有更新,所以表格没有显示额外的行,而且我的甘特显示也没有用完整个高度。

奇怪的是,如果我触摸 jframe 上的窗口句柄(因此即使调整大小 1px),这似乎会强制完全刷新,然后组件会正确显示,中间的表格和右侧的甘特图填满了显示屏。

如果我不调整大小,则当树中的节点展开/折叠时,会调用重新绘制,正如我看到甘特视图更新一样,但它似乎没有展开以占据屏幕的整个高度。

我在下面放置了一个屏幕截图,其中顶部显示了调整大小()后的正确显示,下半部分显示了如果我扩展节点而不进行调整大小,我会看到的结果。

我的断言是组件知道如何正确地重新绘制()本身,因为它在调整大小时做了正确的事情,但这必须触发一个特殊事件来强制更全面的刷新。我尝试了 update()、invalidate()、repaint() 的各种组合,但我似乎无法模拟完整的 < code>repaint() 在调整父窗体大小时发生。

Screenshot

谢谢,王牌

I'm building a custom swing component (more as a practical thought experiment than anything else).

I'm having problems with repainting. My component has a jtree, a jtable, and then a part that is totally custom painted (hopefully ending up to be quite MS project-esque ganttview)

The specific problem is that when the jtree expands, the table and the gannt view should update to reflect the new sub projects under the newly expanded treenode. They do this, but the size doesn't update, so the table doesn't show the additional rows, and my gannt display doesn't use up the full height either.

the odd thing is that if I touch the window handle on the jframe (so even a 1px resize) - this seems to force a complete refresh and the component then displays properly, both the table in the middle and the gannt on the right fill up the display.

If I don't do a resize, then repaint is called when the nodes in the tree are expanded/collapsed, as I see the ganttview update, but it doesn't seem to expand to take up the full height of the screen.

I have put in a screenshot below which shows at the top the correct display after a resize(), and the lower half, which is what I see if I expand a node without doing a resize.

My assertion is that the component knows how to to repaint() itself correctly, as it does the right thing on resize, but this must be triggering a special event that forces a fuller refresh. I have tried various combinations of update(), invalidate(), repaint(), but I can't seem to emulate the full repaint() that happens when the parent form is resized.

Screenshot

Thanks, Ace

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

吃兔兔 2024-10-21 04:24:08

奇怪的是,如果我触摸 jframe 上的窗口句柄(因此即使调整大小 1px) - 这似乎会强制完全刷新,然后组件会正确显示,

当调整框架大小时,组件将重新验证并重新绘制。因此,您需要在代码中的某个位置(表和 gannt 视图更新的位置)添加:

customComponent.revalidate();
customComponent.repaint();

the odd thing is that if I touch the window handle on the jframe (so even a 1px resize) - this seems to force a complete refresh and the component then displays properly,

When a frame is resized the components are revalidated and repainted. So somewhere in your code (where ever the table and gannt view are updated) you need to add:

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