Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setSize() v/s setPreferredSize() and pack() [duplicate]

I am not clear about what is the difference between setSize() and setPreferredSize(). what would happen if i use setSize() instead of setPreferredSize().


And what exactly does pack() method do ?

like image 749
program-o-steve Avatar asked Jun 17 '11 10:06

program-o-steve


People also ask

What is the difference between setSize and setPreferredSize?

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.

What does pack () do in swing?

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

What happens if setSize () is not called on a JFrame?

What happens if setSize is not called on a window? 1) The window is displayed at its preferred size. 2) It is a syntax error. 3) The window is not displayed.

Which of the following is correct method to change size of a JFrame?

Change window Size of a JFrame To resize a frame, There is a method JFrame. setSize(int width, int height) which takes two parameters width and height.


1 Answers

Calling pack() on a window will size it based on the preferredSize of its containing components. It will be as small as possible but taking into account the preferredSize and layout of its components. 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.

setSize() sets the size of the component and 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 ...

See Java: Difference between the setPreferredSize() and setSize() methods in components

like image 144
Suhail Gupta Avatar answered Sep 24 '22 04:09

Suhail Gupta