setLocationRelativeTo

Discussion in 'Java' started by Allan Valeriano, Feb 22, 2007.

  1. 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
     
    Allan Valeriano, Feb 22, 2007
    #1
    1. Advertising

  2. On Feb 23, 12:54 am, "Allan Valeriano" <> wrote:
    > 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.
     
    Andrew Thompson, Feb 22, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendrik Maryns
    Replies:
    1
    Views:
    1,513
    Hendrik Maryns
    Apr 8, 2008
Loading...

Share This Page