[java] What does .pack() do?

I am learning about JPanel and GridLayout , this snippet of code will produce a simple JPanel with 6 buttons

package testing;


import java.io.*;
import java.util.*;
import java.security.*;
import javax.xml.bind.DatatypeConverter;
import java.lang.*;
import java.awt.*;
import javax.swing.*;

public class Testing 
{

    public static class GridPanel extends JPanel 
    {
        public GridPanel()
        {
            setLayout(new GridLayout(2,3));
            setBackground(Color.GREEN);
            this.setPreferredSize(new Dimension(500,500));

            JButton b1 = new JButton ("Button 1");
            JButton b2 = new JButton ("Button 2");
            JButton b3 = new JButton ("Button 3");
            JButton b4 = new JButton ("Button 4");
            JButton b5 = new JButton ("Button 5");
            JButton b6 = new JButton ("Button 6");

            add(b1);
            add(b2);
            add(b3);
            add(b4);
            add(b5);
            add(b6);
        }

    }



    public static void main(String[] args) 

    {
       GridPanel gp = new GridPanel();
       JFrame jf = new JFrame();
       jf.add(gp);
       jf.pack(); //code wouldnt work if i comment out this line
       jf.setVisible(true);

    }

}

I am wondering why my code wouldnt work if i comment out jf.pack()

This question is related to java swing jpanel

The answer is


The pack() method is defined in Window class in Java and it sizes the frame so that all its contents are at or above their preferred sizes.


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

You should also refer to Javadocs any time you need additional information on any Java API


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to swing

Calling another method java GUI Read input from a JOptionPane.showInputDialog box Call japplet from jframe Java JTable getting the data of the selected row What does .pack() do? How to add row of data to Jtable from values received from jtextfield and comboboxes How can I check that JButton is pressed? If the isEnable() is not work? Load arrayList data into JTable How to draw a circle with given X and Y coordinates as the middle spot of the circle? Simplest way to set image as JPanel background

Examples related to jpanel

What does .pack() do? How to draw a circle with given X and Y coordinates as the middle spot of the circle? Simplest way to set image as JPanel background How to layout multiple panels on a jFrame? (java) add controls vertically instead of horizontally using flow layout JPanel vs JFrame in Java Align text in JLabel to the right Adding JPanel to JFrame Java :Add scroll into text area JPanel setBackground(Color.BLACK) does nothing