How in Java to find the size and coordinates of the area (the rectangle in fact) in the JFrame, where can I draw?
That is, not counting the border JFrame and its upper part.
2 answers
I solved the problem by adding two lines of code before knowing the size of the available space.
this.setVisible(true); this.setVisible(false); I do not know why, but it works that way. After adding this.setVisible(true); size becomes known. Immediately after that I use this.setVisible(false); because Further components added to the panel are not displayed. Of course, at the end I use this.setVisible(true); again this.setVisible(true);
|
To get the size of the area, use:
frame.getContentPane().getSize(); Before this, you must have frame.pack()
For coordinates, use the JFrame getLocation() or getLocationOnScreen() methods that inherit from java.awt.Component.
- When I use frame.pack (), the window becomes zero size, and I need a certain size. - Nikolay
|