Forums Register Login

GUI Question - Centering Frame and Thread safety

+Pie Number of slices to send: Send
Hi all,
I'm currently working on centering my main frame, and have a question regarding thread safety in swing. Is it safe to make a call to center the frame between the pack() and setVisible() methods. According to the Sun Tutorial...
The components in the GUI are realized by the pack call.
Immediately afterward, the components in the GUI are shown with the setVisible (or show) call. Technically, the setVisible call is unsafe because the components have already been realized by the pack call. However, because the program doesn't already have a visible GUI, it's exceedingly unlikely that a paint request will occur before setVisible returns.
If I write my code as follows:-
frame.pack()
frame.setVsible(true)
frame.setLocationRelativeTo(null)
Then the window will first appear in the top corner, then move to the center which looks ugly
re-ordering the code to
frame.pack()
frame.setLocationRelativeTo(null)
frame.setVisible(true)
will get rid of the window movement glitch, but is this an unsafe way of doing it?
Any comments, thoughts, feedback gratefully accepted.
Thanks,
Paul
I miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 562 times.
Similar Threads
Centering Window
Creating and showing JFrame from another JFrame
centering a dialog
Always centering frame through a BaseFrame overiding setVisible: dirty ?
Centering dialogs in a multi-monitor environment?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 04:05:38.