3

I've written a Swing application. I want to set the JFrame to be centred in the user's screen. I can set it to the approximate center of my screen using:

JFrame frame = new JFrame("Test Frame");
Rectangle r = new Rectangle(600, 300, 400, 400);
frame.setBounds(r);

Is there a way to define the Rectangle r such that the center of it is at the center of any screen?

Is setBounds() the wrong method for varying the frame position dynamically?

Miguel
  • 1,966
  • 2
  • 18
  • 32
  • Aaaaargh! Use [`setLocationByPlatform(true)`](http://stackoverflow.com/questions/7143287/how-to-best-position-swing-guis/7143398#7143398) to make a GUI that does ***not*** mimic a splash screen. – Andrew Thompson Aug 04 '12 at 23:16
  • Using that places the GUI off-centre, which is precisely what I don't want. This GUI was made simply so it could be run without using the commandline, but still have inputs. Looks are not important other than the GUI being centred in the screen. Oddly, I was looking at [this](http://stackoverflow.com/questions/7143287/how-to-best-position-swing-guis) question just before I saw your comment. –  Aug 04 '12 at 23:31
  • *"which is precisely what I don't want"* Unless you are the only user, I fail to see the relevance of that comment. – Andrew Thompson Aug 04 '12 at 23:35
  • I won't be the only user but this application will not be going outside of my department. What could go wrong with using `setLocationRelativeTo(null)`? This seems to work to what I want but for future reference I'm interested. –  Aug 04 '12 at 23:42
  • Yes it works, but it looks unprofessional. I thought I'd covered that in my answer to the linked question. Of course it comes down to a matter of opinion, so if you are not convinced of mine I'll leave it at that. Glad you got it sorted to the stated spec. – Andrew Thompson Aug 04 '12 at 23:45
  • I'm not experienced enough in creating Java GUIs to have an informed opinion so I'll do as you suggest, having thought about your original comment. –  Aug 04 '12 at 23:52
  • 1
    Cool. I am converting people over to using `setLocationByPlatform` one at a time. ;) – Andrew Thompson Aug 04 '12 at 23:54

1 Answers1

4

Do you want to center a JFrame in the screen? If so then simply call setLocationRelativeTo(null) on the JFrame after calling pack(). You really don't want to set the size of the JFrame if you can avoid it, but instead have your layout managers do the size setting for you. The pack() method will ask the containers' layout managers to lay out their components and set the best sizes for their components.

Edit
Regarding your question:

Thanks, that's great. Any chance you could tell me where I could find the method details within docs.oracle

Since this method can be called on a JFrame, simply go to the JFrame API. If this is a method of a parent class, the API will tell you and provide the link to the parent class and its method.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Thanks, that's great. Any chance you could tell me where I could find the method details within docs.oracle –  Aug 04 '12 at 19:38
  • 1
    Buried within inherited methods from `java.awt.Window`. –  Aug 04 '12 at 19:46
  • @Sean: learning to drive the API is an invaluable skill; as an exercise, try looking up methods mentioned above in the index. – trashgod Aug 04 '12 at 19:46
  • @trashgod Agreed, I'm fairly new to Java and have not got round to it, to my detriment. –  Aug 04 '12 at 19:47