111

What is the main difference between setSize() and setPreferredSize(). Sometimes I used setSize(), sometimes setPreferredSize(), sometimes one does what I want, sometimes the other.

What call should I use for JFrames and JPanels?

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
David Robles
  • 9,477
  • 8
  • 37
  • 47

5 Answers5

127

Usage depends on whether the component's parent has a layout manager or not.

  • setSize() -- use when a parent layout manager does not exist;
  • setPreferredSize() (also its related setMinimumSize and setMaximumSize) -- use when a parent layout manager exists.

The setSize() method probably won't do anything if the component's parent is using a layout manager; the places this will typically have an effect would be on top-level components (JFrames and JWindows) and things that are inside of scrolled panes. You also must call setSize() if you've got components inside a parent without a layout manager.

Generally, setPreferredSize() will lay out the components as expected if a layout manager is present; most layout managers work by getting the preferred (as well as minimum and maximum) sizes of their components, then using setSize() and setLocation() to position those components according to the layout's rules.

For example, a BorderLayout tries to make the bounds of its "north" region equal to the preferred size of its north component---they may end up larger or smaller than that, depending on the size of the JFrame, the size of the other components in the layout, and so on.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Sbodd
  • 11,279
  • 6
  • 41
  • 42
  • 1
    I thought a component was using a layout manager by default (BorderLayout?), so if I don't explicitly set a layout manager does that mean I should use setSize() instead of setPreferredSize()? – David Robles Nov 23 '09 at 15:35
  • 3
    I believe JPanels use BorderLayout by default, but JComponent has no default layout. Most of the time, you're better off setting a layout manager if you'll be adding something instead of using setSize(). – Sbodd Nov 23 '09 at 16:05
  • 10
    My recommendation is to always use a layout manager. SetSize() should be thought of as something that the layout manager calls - not something that you call. – Kevin Day Nov 24 '09 at 04:05
  • 9
    @Sbodd - JPanels have a FlowLayout by default. JFrame.getContentPane() has a BorderLayout by default. – Nemi Nov 25 '09 at 00:30
  • By the way one useful fact is that if you e.g. setPreferredSize() of a Frame to something very wide, e.g. 1920 pixels, then someone running in e.g. 1600 resolution will still receive a window that "fits their screen". That's the question I came here looking for, and since I couldn't find the explicit answer on SO I "took one for the team" and tested it manually :) -- so anyway thought I'd at least mention that here. – Brian Jan 13 '22 at 17:42
11

setSize() or setBounds() can be used when no layout manager is being used.

However, if you are using a layout manager you can provide hints to the layout manager using the setXXXSize() methods like setPreferredSize() and setMinimumSize() etc.

And be sure that the component's container uses a layout manager that respects the requested size. The FlowLayout, GridBagLayout, and SpringLayout managers use the component's preferred size (the latter two depending on the constraints you set), but BorderLayout and GridLayout usually don't.If you specify new size hints for a component that's already visible, you need to invoke the revalidate method on it to make sure that its containment hierarchy is laid out again. Then invoke the repaint method.

0x6C38
  • 6,796
  • 4
  • 35
  • 47
Varun
  • 1,014
  • 8
  • 23
7

setSize will resize the component to the specified size.

setPreferredSize sets the preferred size. The component may not actually be this size depending on the size of the container it's in, or if the user re-sized the component manually.

Glen
  • 21,816
  • 3
  • 61
  • 76
5

IIRC ...

setSize sets the size of the component.

setPreferredSize sets the preferred size. The Layoutmanager will try to arrange that much space for your component.

It depends on whether you're using a layout manager or not ...

miku
  • 181,842
  • 47
  • 306
  • 310
  • and what if I add two jpanels to a jframe without specifying a layout manager explicitly for the jframe? what method should i used to set the size of the jpanels? – David Robles Nov 23 '09 at 15:32
  • 3
    Content panes use BorderLayout by default (http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html). So the JFrame's contentpane has a LayoutManager, so setPreferredSize _should_ work .. – miku Nov 23 '09 at 15:36
  • Sounds good, that means I should use setSize for the JFrame and setPreferredSize for the components inside – David Robles Nov 23 '09 at 15:38
  • 1
    You would generally use frame.pack() to take advantage of the calculated preferred size of all the child components. Otherwise if you just randomly use frame.setSize(), then the components added to the content pane will expand/contract to fit the space available, which means the preferred size of each component may be overridden. – camickr Nov 23 '09 at 17:05
0

In addition to the fact that setSize() should be used in the absence of a layout manager, and setPreferredSize() - when there is no layout manager, there is another difference. 1 takes two numbers in the parameters, and setPreferredSize() takes an object of class java.awt.Dimension:

JFrame frame = new JFrame("Test");
frame.setSize(200,300); //numbers
frame.setPreferredSize(new java.awt.Dimension(200,300)); //an object of java.awt.Dimension
Зонтик
  • 101
  • 6