user4220748
user4220748

Reputation:

Can't get size of an ExtendedState JFrame

I'm not able to get the size of a JFrame. The JFrame has been maximized with setExtendedState(MAXIMIZED_BOTH) and it is also visible.

import javax.swing.JFrame;

public class Fenster extends JFrame {

    Fenster() {

        this.setExtendedState(MAXIMIZED_BOTH);
        this.setUndecorated(true);
        this.setAlwaysOnTop(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.pack();
        this.setVisible(true);
        System.out.println(this.getWidth() + " x " + this.getHeight());

    }

}

I only get "0 x 0" or "1 x 1" in the console. And I also dont know why the output is random.

Upvotes: 0

Views: 124

Answers (2)

copeg
copeg

Reputation: 8348

JFrame.pack() is called after setExtendedState which may resize the JFrame (others comment not to experience this issue, so it may be environment based). I can reproduce what you observe on a Mac - if pack() is removed or setExtendedState called after pack() the behavior seems more in tune with what I believe you want.

Upvotes: 2

TameHog
TameHog

Reputation: 951

Try:

int w = getSize().getWidth();
int h = getSize().getHeight();

I don't know why your code does not work

Upvotes: 0

Related Questions