Pawel Debski – SW Development and other thoughts Java, Microsoft.Net C#, TIBCO, WebMethods, Oracle, Informatica

Java Swing setMaximumSize does not work

A friend studying IT got an exercise to write a nice & simple app in #Java #Swing to draw a few things. He did quite a nice coding and the app works like a charm. He did not manage however to do one thing: #limit the #maximum #size of the #window and he asked me, the famous consultant and top-notch developer, for help. Being a superhero I could not refuse – my blazing-fast fingers touched the keyboard just like I’d be Chopin and… bdzionk. It did not work. #setMaximumSize (new #Dimension (max_x, max_y)) simply does not work for me. After few hours of trying and googling I found a #workaround which is not elegant and does not look good to user. I still have no right #solution. Perhaps you, my dear network, can help me with it? Or at least explain why it does not work?

Here are the details:

public class SwingExercise

extends JApplet

implements Runnable // to update some simple animation


    // some variables go here

    // including timer, action listeners, etc.

    public SwingExercise() throws HeadlessException {


        // this works…

        this.setMinimumSize(new Dimension(min_x, min_y));

        // this does not

        this.setMaximumSize(new Dimension(max_x, max_y));

        // this also works…

        this.setPreferredSize(new Dimension(initial_x, initial_y));


    public void paint(Graphics g) {


        Graphics2D g2 = (Graphics2D) g;

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        this.wndSz = getSize();   


            System.out.print(“Window size: t”);


            boolean isMaximumSS=this.isMaximumSizeSet();

            System.out.print(“isMaximumSizeSet? t”);



                System.out.print(“Max window size: t”);




        // other painting goes here


    private void createAndShowGUI() {


        JFrame frame = new JFrame(“Swing Exercise”);


        frame.getContentPane().add(“Center”, this);




    public void run() {




    public static void main(String[] args) {

        javax.swing.SwingUtilities.invokeLater(new SwingExercise());



Here is the console output after some resizing:

Window size: java.awt.Dimension[width=858,height=467]

isMaximumSizeSet? true

Max window size: java.awt.Dimension[width=800,height=400]

I gaped on this output for some time and tried to apply setMaximumSize to all the components I could find, including JFrame, frame.getContentPane() but nothing worked. I found another solution with no use of setMaximumSize, but I do not like it. Really, we shall make setMaximumSize work somehow.


Written by Pawel Debski

2013/05/25 at 21:34

%d bloggers like this: