Hi I am developing an desktop application using swing when i am executing the frame window it is displaying at the left side bottom of my screen can i customize that and display at in center of the screen?
Asked
Active
Viewed 150 times
1
-
`frame.setLocationRelativeTo(null)`? – Paul Samsotha Feb 18 '14 at 09:17
-
actually i cant able to edit the generated code confused to insert this code – Suresh Feb 18 '14 at 09:20
-
Are you using GUI Builder? – Paul Samsotha Feb 18 '14 at 09:21
-
yes i am using gui builder am setting all the properties through that only – Suresh Feb 18 '14 at 09:23
2 Answers
3
"yes i am using gui builder"
Just put setLocationRelativeTo(null)
after initComponents()
, in the constructor.
public class MyFrame extends JFrame {
public MyFrame() {
initComponents();
setLocationRelativeTo(null);
}
}
JFrame
as a subclass of Window
inherits Window
methods. The API can be seen here The method used above is listed as follows
public void setLocationRelativeTo(Component c)
- Sets the location of the window relative to the specified component according to the following scenarios.
If the component is null, or the GraphicsConfiguration associated with this component is null, the window is placed in the center of the screen.

Paul Samsotha
- 205,037
- 37
- 486
- 720
-
For frame positioning, you cannot go by `setLocationByPlatform(true)`. See [this answer](http://stackoverflow.com/a/7143398/418556) for demo. ;) – Andrew Thompson Feb 18 '14 at 10:03
-
@AndrewThompson is your comment worded correctly? I'm a bit confused by it and its implication :/ – Paul Samsotha Feb 18 '14 at 10:08
-
Well, it presumed, *"No **don't** set it in the middle of the screen, that looks so 'Splash Screenish'! Instead.."* was understood. – Andrew Thompson Feb 18 '14 at 11:00
-
@AndrewThompson _"you cannot go by setLocationByPlatform(true)"_ .. Did you mean "you **can**" ? – Paul Samsotha Feb 18 '14 at 11:11
-
1Oh, sorry, *"cannot go by"* means *"cannot pass up"* or *"cannot do without"*. :P – Andrew Thompson Feb 18 '14 at 11:52
0
Following Code will provide you Width and Height of Your Screen
Toolkit toolkit=Toolkit.getDefaultToolkit();
toolkit.getScreenSize().width
toolkit.getScreenSize().height
Then Using This Height and Width You can set where to position Your frame with following function
this.setLocation(int x, int y)
//first variable x is position relative to left top of ur screen in horizontal direction
// second variable y is position relative to left top in vertical direction

user987339
- 10,519
- 8
- 40
- 45

user3386797
- 3
- 4