9

Is it possible to set fixed width for JFrame? At least the following code isn't working:

import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;

public final class Main  {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame f = new JFrame();
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setSize(500, 500);
                f.setMinimumSize(new Dimension(500, 0));
                f.setMaximumSize(new Dimension(500, Integer.MAX_VALUE));
                f.setVisible(true);
            }
        });
    }

}

The minimum setting is working but the maximum is not.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
perak
  • 1,310
  • 5
  • 20
  • 31

4 Answers4

20

Use f.setPreferredSize(new Dimension(500, 500)); in your code. And definitely read this post : Java: Difference between the setPreferredSize() and setSize() methods in components And to prevent your frame growing or shrinking use f.setResizable(false);

Community
  • 1
  • 1
Juvanis
  • 25,802
  • 5
  • 69
  • 87
2

Set size then write:

JFrame.setResizable(false);
ElmoVanKielmo
  • 10,907
  • 2
  • 32
  • 46
0

Just set the maximum to the value you want to specify. Depending... the maximum for screen size or our own value instead of Integer.MAX_VALUE

0

you can use

Rectangle rc = f.getBounds();
rc.width = 500; //set max width
rc.height = Integer.MAX_VALUE; // set max height
f.setMaximizedBounds(rc);

Hope it works on you