setPreferredSize does not work

38,536

Solution 1

As you said, pack() will try and arrange the window so that every component is resized to its preferredSize.

The problem is that it seems that the layout manager is the one trying to arrange the components and their respective preferredSizes. However, as you set the layout manager as being null, there is no one in charge of that.

Try commenting the setLayout(null) line, and you're gonna see the result. Of course, for a complete window, you're going to have to choose and set a meaningful LayoutManager.

This works fine to me:

import java.awt.Dimension;

import javax.swing.*;

public class Game extends JFrame {
    private static final long serialVersionUID = -7919358146481096788L;
    JPanel a = new JPanel();
    public static void main(String[] args) {
        new Game();
    }
    private Game() {
        setTitle("Insert name of game here");
        setLocationRelativeTo(null);
        //setLayout(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        a.setPreferredSize(new Dimension(600, 600));
        add(a);
        pack();
        setVisible(true);
    }
}

Solution 2

pack() queries the preferred size of the parent container over that of the child so you would have to use:

setPreferredSize(new Dimension(600, 600));

Another note is to call

setLocationRelativeTo(null);

after pack() has been called to calculate center coordinates :)

OK, just spotted the null layout there, why not use the default BorderLayout of JFrame?

Solution 3

Your problem is setLayout(null), becase the docs say for pack():

Causes this Window to be sized to fit the preferred size and layouts of its subcomponents

thus with no layout it does not execute correctly.

This seems to work fine for me:

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Game extends JFrame {

    JPanel panel = new JPanel();

    private void createAndShowGUI() {
        setTitle("FrameDemo");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel.setPreferredSize(new Dimension(600, 600));
        add(panel);

        //setLayout(null); //wont work with this call as pack() resizes according to layout manager
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Game().createAndShowGUI();
            }
        });
    }
}
Share:
38,536
tckmn
Author by

tckmn

https://tck.mn Elected moderator ♦ on Code Golf. Email [email protected] or find me in chat: The Nineteenth Byte (for Code Golf), The Sphinx's Lair (for Puzzling), :chat! (for Vi and Vim), and SE Chess (informal group in which I lose a lot).

Updated on August 14, 2020

Comments

  • tckmn
    tckmn over 3 years

    Code:

    import java.awt.Dimension;
    
    import javax.swing.*;
    
    public class Game extends JFrame {
        private static final long serialVersionUID = -7919358146481096788L;
        JPanel a = new JPanel();
        public static void main(String[] args) {
            new Game();
        }
        private Game() {
            setTitle("Insert name of game here");
            setLocationRelativeTo(null);
            setLayout(null);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            a.setPreferredSize(new Dimension(600, 600));
            add(a);
            pack();
            setVisible(true);
        }
    }
    

    So I set the preferred size of the JPanel to 600 by 600 and pack the frame, but the frame's size is still 0 by 0.

    Why is this and how do I fix it?

  • tckmn
    tckmn over 11 years
    okay thanks that works, but now whatever I add to the JPanel does not appear!
  • nIcE cOw
    nIcE cOw over 11 years
    +1 for asking to change the sequence, in which calls are being made. First add components to the JFrame, so that it's size can be realized, then guide it's position on the screen relative to it's size, and then set it to visible. good approach :-)