Java setPreferredSize() fails

3519 views java

This is my very first attempt working with Java so please go easy on me. I have looked at the other JFrame questions and I simply have not been able to find a working solution.

For some reason, I cannot get setPreferredSize or setSize to work.

The window continues to default to a different size. setMinimumSize seems to work just fine. setMaximumSize appears to be bugged from my reading.

Ultimately I am just trying to set the window to a specific non changeable size.

    public LauncherFrame(@NonNull Launcher launcher) {
        super(tr("launcher.title", launcher.getVersion()));

        this.launcher = launcher;
        instancesModel = new InstanceTableModel(launcher.getInstances());

        setPreferredSize(new Dimension(900, 600));
        setMinimumSize(new Dimension(600, 300));

        SwingHelper.setFrameIcon(this, Launcher.class, "icon.png");

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {

answered question

1 Answer


You are calling pack(); which will pack the frame to a different size, Just remove the pack(); call.

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

for more exposure look at this post What does .pack() do?

posted this

Have an answer?


Please login first before posting an answer.