I'm building a custom swing component (more as a practical thought experiment than anything else).开发者_JAVA技巧
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.
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,
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();
精彩评论