setLocationRelativeTo

A

Allan Valeriano

Hi all,

I have a class that extends JFrame, and on its constructor, I have the
2 lines of code:

pack();
setLocationRelativeTo(owner);

I'd like to know what would be the difference if I swap those 2
lines...
It seems that if I use:

setLocationRelativeTo(owner);
pack();

the frame gets wrongly aligned but if I put the pack before
setLocationRelativeTo, it gets perfectly centered with the mother
window.
Does anybody knows what is the difference for java when I swap those
lines?



thanks in advance
Allan Valeriano
 
A

Andrew Thompson

Hi all,

I have a class that extends JFrame, and on its constructor, I have the
2 lines of code:

pack();
setLocationRelativeTo(owner);

I'd like to know what would be the difference if I swap those 2
lines...
It seems that if I use:

setLocationRelativeTo(owner);
pack();

the frame gets wrongly aligned but if I put the pack before
setLocationRelativeTo, it gets perfectly centered with the mother
window.

Given that pack()..
"Causes this Window to be sized to fit
the preferred size and layouts of its
subcomponents. "
...I consider your result unsurprising.

I would generally pack() a frame before
it is setVisible(true), let alone before
attempting to set its location.
Does anybody knows what is the difference for java when I swap those
lines?

One works as advertised, the other is
expected (by my reading of the JavaDocs)
to produce unsatisfactory results.

Call pack() first, then the setLocationRelativeTo
method can have a good idea how big the (J)Frame
needs to be (and 'centering' it based on the
position of other components becomes anything
more than guesswork).

Andrew T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top