1

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());

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        initComponents();
        setPreferredSize(new Dimension(900, 600));
        setMinimumSize(new Dimension(600, 300));
        setResizable(false);
        pack();
        setLocationRelativeTo(null);

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

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                loadInstances();
            }
        });
    }

* UPDATE *

WOW ok... I am an idiot!!! The reason the sizing was not working was because the size was being set externally in another called file... Thank you everyone for the help. I have marked what would have been the correct answer.

Atomiklan
  • 5,164
  • 11
  • 40
  • 62

2 Answers2

3

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?

How ever if it still fails for you, you can try using setSize()

    setPreferredSize(new Dimension(900, 600));
    setSize(new Dimension(900, 600));
    setMinimumSize(new Dimension(600, 300));
SamHoque
  • 2,978
  • 2
  • 13
  • 43
0

You are calling the initComponents() method before you set the size of your window.

This way, you are drawing the whole window and you are setting the size after that, causing the size that you set to be ignored.

My suggestion would be to pass 2 Dimension objects to the initComponents method; 1 for preferredSize and one for minimumSize.

Then, set the size first and draw to the screen after that.

Hope this helped.

Soutzikevich
  • 991
  • 3
  • 13
  • 29
  • It would really help if your code had meaningful comments. This is the reason commenting exists in the first place. Maybe try `launcher.setSize()` (*or whatever your frame is that you are trying to set the size for*), instead of calling just `setSize()`. **REMEMBER** you have to set the size before you draw the window or the panel, so that it will be correctly rendered – Soutzikevich Nov 06 '18 at 10:48
  • Nope, that fails too. Also, turns out initComponents was not affecting it. I removed those sections of code and the window still refuses to set to a specified size. – Atomiklan Nov 06 '18 at 11:12
  • Well, if you want you can add comments to your code and post it here and I'll have a look to see what I can do to help. – Soutzikevich Nov 06 '18 at 11:14
  • Will try tomorrow. Sun is coming up haha. Should probably go to bed. Thank you for the help! – Atomiklan Nov 06 '18 at 11:38