49

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

sidgate
  • 14,650
  • 11
  • 68
  • 119
Computernerd
  • 7,378
  • 18
  • 66
  • 95
  • 8
    Take a look at the [javadoc](http://docs.oracle.com/javase/8/docs/api/java/awt/Window.html#pack) – robertoia Apr 10 '14 at 08:12
  • `this.setPreferredSize(new Dimension(500,500));` See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Apr 10 '14 at 08:17
  • `jf.pack(); //code wouldnt work if i comment out this line` Sure it works, but since the frame has a size of 0x0 it is not easy to *see* it works. – Andrew Thompson Apr 10 '14 at 08:18
  • @Computernerd (I sure that there) I'm missing here answer about pack() together with LayoutManager, PreferredSize, getPreferredSize that returns JComponents to container, this logics is quite alchemy, is not possible to answering in short answer, and (looks like as) your question isn't good described – mKorbel Apr 10 '14 at 08:23
  • @Computernerd and each of LayoutManagers (custom too) handling diferrently with min, max and PreferredSize, for some required, and for some ignored – mKorbel Apr 10 '14 at 08:27
  • Reopen: the documentation linked is inadequate (e.g., it doesn't contain an example). – Evgeni Sergeev Oct 12 '18 at 18:30

2 Answers2

71

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

sidgate
  • 14,650
  • 11
  • 68
  • 119
0

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

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
Gaurav
  • 19
  • 3