J.Olufsen
J.Olufsen

Reputation: 13915

How to check current window size in java swing?

For instance, the size of the window changed (user resized it), how to get current window size?

Upvotes: 11

Views: 68070

Answers (3)

mort
mort

Reputation: 13608

Simply use the getSize()method: javadoc

Upvotes: 3

williamg
williamg

Reputation: 2758

Assuming you have a JFrame in which you are drawing your interface:

Dimension size = frame.getBounds().getSize()

Returns the dimensions of the frame. Additionally, you can give the frame a resize handler to catch whenever the user adjusts the frame's size:

    class ResizeListener implements ComponentListener {

        public void componentHidden(ComponentEvent e) {}
        public void componentMoved(ComponentEvent e) {}
        public void componentShown(ComponentEvent e) {}

        public void componentResized(ComponentEvent e) {
            Dimension newSize = e.getComponent().getBounds().getSize();          
        }   
    }

    frame.addComponentListener(new ResizeListener());

Upvotes: 8

Jan Vorcak
Jan Vorcak

Reputation: 20029

Rectangle r = frame.getBounds();
h = r.height;
w = r.width;

Upvotes: 24

Related Questions