0

When I create a JFrame with child components, it opens to the smallest possible width a window can be, when I expected the width to be the minimum size to display the contents of the window without horizontal scroll bars.

How do I determine what that minimum size should be? Obviously something in the UI knows, because when made wide enough, the horizontal scroll bar disappears. This is the state I want the window when created. How do I fetch that number, which I suspect I would return as an override to getPreferredSize? As far as a preferred height, that's arbitrary.

Matthew Reddington
  • 1,409
  • 3
  • 13
  • 24

1 Answers1

3

As much as I know, when you use the pack() method, it automatically chooses the minimum size needed for all the components in-hand.

In another question, this subject is covered:

The pack method sizes the frame so that all its contents are at or above their preferred sizes. An alternative to pack is to establish a frame size explicitly by calling setSize or setBounds (which also sets the frame location). In general, using pack is preferable to calling setSize, since pack leaves the frame layout manager in charge of the frame size, and layout managers are good at adjusting to platform dependencies and other factors that affect component size.

From Java tutorial

Community
  • 1
  • 1
MordechayS
  • 1,552
  • 2
  • 19
  • 29