开发者

Resizing JFrame when child resizes: how to keep up?

I'm trying to work with a JFrame (actually, Frame in Scala's Swing wrapper, though I don't see why this should matter) that contains a BoxLayout which has several items, one of which changes size in response to a button click (another item in the BoxLayout).

When resizing, the button event handler does the following:

  1. Call a method on the item (JPanel) that changes size; this calls setPreferredSize for that component.
  2. Call pack on the JFrame
  3. Call repaint on the JFrame

This works with one very strange caveat: the JFrame is always one step behind. So if I set my preferred x sizes to 100, 200, 300, 200, 100 the JFrame will have width 100, 100, 200, 300, 200.

What might be going wrong, and how do I get the开发者_如何转开发 size changes to propagate out to the JFrame window size when they're supposed to?

(More possibly but hopefully not relevant details: Ubuntu 10.10 with Sun Java 1.6.0_24.)


When you change the size of a component I would try to revalidate() the parent container so the layout manager can be invoked again which will recalculate all the sizes properly.

If you need more help then post your SSCCE that demonstrates the problem. And first try this with a JFrame so if there is still a problem you will know if the standard JDK classes or Scala is causing the problem.


I agree with camickr that revalidate() must be used instead of pack(). To fix the "one step behind" try to call revalidate() and repaint() in SwingUtilities.invokeLater().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜