How to check current window size in java swing?

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

Simply use the getSize()method: javadoc


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

Tags:

Java

Swing