For instance, the size of the window changed (user resized it), how to get current window size?
-
"how to get current window size?" Why do you think you need it? I've written 100s of GUIs and rarely needed to get the size of the parent container (especially not on resize).– Andrew ThompsonNov 30, 2011 at 21:38
-
2How you handle resizing of frame then?– GiannisMar 27, 2012 at 13:37
Add a comment
|
3 Answers
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());
-
3if you don't override most of the methods, I'd consider using ComponentAdaper instead Dec 1, 2011 at 17:08
Simply use the getSize()
method: javadoc
-
3I'm getting 404 on that link. Please update it and complement your answer, so this doesn't invalidate it in the future.
getSize()
method from which class? Jul 19, 2018 at 11:38 -
1@VictorMoraes since OP was afk for a while, I have updated broken link.– pavjelApr 23, 2019 at 14:36