SeniorJD
SeniorJD

Reputation: 7202

Swing components twitch when resizing JFrame

I have an undecorated JFrame which has a lot of components inside it (such as JSplitPanes, JPanels with GridBagLayouts, BoxLayouts, BorderLayouts etc). The code of building this JFrame is 2500 lines length, so I wouldn't place it here, or it's simpled version, sorry.

When I drag the JFrame by right or bottom side, it resizes OK, but when I drag it by left or top side, the components inside the JFrame are twitching a lot, so it looks very ugly.

My question is: Why does it happen? How can I prevent it (if I can)? Does anyone fix that in own practice?

UPD: I've written my own resizer for JFrame. It works OK for other windows, which have less amount of components.

Upvotes: 0

Views: 200

Answers (1)

trashgod
trashgod

Reputation: 205785

Resizing the frame implicitly validates the enclosed Container, which causes doLayout() to be invoked on the affected components each time the size changes. You can see a similar effect in repeated calls to paintComponent() when resizing this AnimationTest. As you observe, using thousands of components scales poorly. As an alternative, leverage the flyweight pattern to render only visible cells, as is done in JTable, JTree, JList, etc. If you can't use one of these, CellRendererPane, seen here, may help.

Upvotes: 4

Related Questions