Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java setResizable(false) changes the window size (swing)

Tags:

java

window

swing

I have a strange problem. I am using the null layout for a window (= JFrame and on windows) and if I use setResizable (false) the window size gets bigger (to right and bottom, around 10 pixels I would say). I do not know why.

The two println's return the same sizes, what is strange, also...

mainWnd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainWnd.setTitle(wndTitle);
mainWnd.setBounds(wndPosX, wndPosY, wndWidth, wndHeight);
System.out.println(mainWnd.getHeight() + mainWnd.getWidth());
mainWnd.setResizable(false);
System.out.println(mainWnd.getHeight() + mainWnd.getWidth());

Does somebody has an idea? Why does the window gets resized?

UPDATE: Same thing here (compile it with and without the setResizable and than you can see it, if you overlap the windows):

import javax.swing.JFrame;
import javax.swing.JPanel;


public class Main
{
    private static JFrame mainWnd = null;

    public static void main(String[] args)
    {
        mainWnd = new JFrame();

        mainWnd.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainWnd.setTitle("asda");
        mainWnd.setBounds(50, 50, 300, 300);

        mainWnd.setResizable(false);

        mainWnd.setVisible(true);
    }
}
like image 441
immerhart Avatar asked Jun 27 '12 11:06

immerhart


1 Answers

It does not change on my example, so you must have something else that causes your issue:

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Test4 {

    protected static void initUI() {
        JFrame frame = new JFrame("test");
        frame.setBounds(0, 0, 300, 200);
        frame.setVisible(true);
        System.err.println(frame.getSize());
        frame.setResizable(false);
        System.err.println(frame.getSize());
    }

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

            @Override
            public void run() {
                initUI();
            }
        });
    }
}

EDIT/UPDATE:

Somehow insets are incorrect when you set the resizable to false (at least on Windows 7 and JDK 6). Somehow they change from 30,8,8,8 to 25,3,3,3 although the border (which is painted by the OS) stays actually the same. Since insets are part of the bounds of the Frame, the frame is actually too big (visually) when it is not resizable. For me it looks like there is a bug in the computed insets when the frame is not resizable.

like image 128
Guillaume Polet Avatar answered Nov 16 '22 03:11

Guillaume Polet