简体   繁体   English

自定义摇摆组件:repaint()问题

[英]Custom swing component: Problem with repaint()

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) 我的组件有一个jtree ,一个jtable ,然后是一个完全自定义绘制的部分(希望最终成为完全像MS项目一样的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. 特定的问题是,当jtree扩展时,表和结点视图应更新以反映新扩展的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. 奇怪的是,如果我触摸jframe上的窗口句柄(因此即使调整了1px的大小),这似乎也将强制进行完全刷新,然后该组件正确显示,中间的表和右侧的加特都填满了显示器。

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. 如果我不调整大小,则当树中的节点被展开/折叠时,将调用repaint,正如我看到的ganttview更新,但它似乎并未扩展以占据屏幕的整个高度。

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. 我在下面放置了一个屏幕截图,该屏幕截图在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. 我的断言是,该组件知道如何正确地自己repaint(),因为它在调整大小时做正确的事情,但这必须触发一个特殊事件,该事件强制进行更完整的刷新。 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. 我尝试了update (), invalidate (), repaint ()的各种组合,但是我似乎无法模仿父窗体调整大小时发生的完整repaint ()。

屏幕截图

Thanks, Ace 谢谢,王牌

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, 奇怪的是,如果我触摸jframe上的窗口句柄(甚至调整了1px的大小),这似乎会强制进行完全刷新,然后该组件会正确显示,

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: 因此,您需要在代码中的某处(无论在何处更新了表和gannt视图),都需要添加:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM