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 ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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 andsetPreferredSizesets 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