views:

730

answers:

5

Ok, I read the Java Documentation and I just can't figure out what is the main difference between those two methods. Sometimes I used setSize(), sometimes setPreferredSize(), sometimes one does what I want, sometimes the other.

So, what is the main difference between the two? Which one should I use for JFrames and JPanels?

Thanks

+1  A: 

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
+1  A: 

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 ...

The MYYN
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?
davidrobles
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 ..
The MYYN
Sounds good, that means I should use setSize for the JFrame and setPreferredSize for the components inside
davidrobles
According to the docs, i'd say so ...
The MYYN
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
+6  A: 

The short answer is: it's complicated.

The slightly longer answer is: use setSize() if your component's parent has no layout manager, and setPreferredSize() and its related setMinimumSize and setMaximumSize if it does.

setSize() 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.

As a general rule, setPreferredSize() should do the "right thing" if you've got a layout manager; most layout managers work by getting the preferred (as well as minimum and maximum) sizes of their components, and then using setSize() and setLocation() to position those components according to the layout's rules. So (as an example) a BorderLayout will try 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 frame, the size of the other components in the layout, and so on.

Sbodd
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()?
davidrobles
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
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
@Sbodd - JPanels have a FlowLayout by default. JFrame.getContentPane() has a BorderLayout by default.
Nemi
+1  A: 

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.

Varun
A: 

mmm weird... i created a custom panel that extends Panel and i use setSize() to sets it's size...^.^ but when i change this part class InfoPanel extends Panel to this one class InfoPanel extends JPanel

when my custome Panel it's now a custom JPanel setSize() doesn´t work =( and i had to use setPreferredSize() insted! why is that? why is that setSize works fine with Panel and it doesnt with JPanel.

oh by the way, i set the Panel's layoutManager to null, i mean, im not using a LayoutManager for my Panel and i set the location and size of my components with setBounds().

i create and instance of that InfoPanel and i add it to a JFrame. on my JFrame im using the default LayoutManager. i use pack() on JFrame contructor to set the correct size of the application.

what would be the problem? or the explanation for that behavior of setSize() with Panel(it works) and JPanel(doesn't work and with setPreferredSize it does work).

zitfrit