setLocation problem with swing!!!

Discussion in 'Java' started by asd, Feb 7, 2007.

  1. asd

    asd Guest

    Hi all,

    I am doing some kind of home work using swing. Unfortunately I am having
    problems with setLocation() function. I know it is a bad idea to use
    setPosition and I know I should use Layout manager instead of doing that.
    Still I would like to do it with setLocation.

    Here is my problem: I am trying to position a button on a given pixel on the
    screen. To do that I am using setLocation function of the JButton.

    Here is what I did:
    Created a JFrame,
    Created a JPanel, and added it to frame.
    Created a JButton, and added it to panel.
    Called setLocation function of the button. It is not working. The button is
    just created in the center of the window, where layout manager places it.

    What should I do to get it work?

    I would really appreaciate it if you could help me with my problem.
    Sincerely yours.
    asd, Feb 7, 2007
    #1
    1. Advertising

  2. asd wrote:
    > Hi all,
    >
    > I am doing some kind of home work using swing. Unfortunately I am having
    > problems with setLocation() function. I know it is a bad idea to use
    > setPosition and I know I should use Layout manager instead of doing that.
    > Still I would like to do it with setLocation.
    >
    > Here is my problem: I am trying to position a button on a given pixel on the
    > screen. To do that I am using setLocation function of the JButton.
    >
    > Here is what I did:
    > Created a JFrame,
    > Created a JPanel, and added it to frame.
    > Created a JButton, and added it to panel.
    > Called setLocation function of the button. It is not working. The button is
    > just created in the center of the window, where layout manager places it.
    >
    > What should I do to get it work?
    >
    > I would really appreaciate it if you could help me with my problem.
    > Sincerely yours.
    >
    >


    From the docs:

    JPanel

    public JPanel()

    Creates a new JPanel with a double buffer and a flow layout.

    So it puts the button where it wants to. Just create the JPanel with a
    null layout manager. Then you can put it anywhere you want. Of course
    if that's all you do you won't be able to see the JButton because it
    doesn't have a size. So call setSize(button.getPreferredSize()) to make
    it appear correctly, more or less.

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Feb 7, 2007
    #2
    1. Advertising

  3. asd

    asd Guest

    "Knute Johnson" <> wrote in message
    news:Fvbyh.251073$...
    > asd wrote:
    >> Hi all,
    >>
    >> I am doing some kind of home work using swing. Unfortunately I am having
    >> problems with setLocation() function. I know it is a bad idea to use
    >> setPosition and I know I should use Layout manager instead of doing that.
    >> Still I would like to do it with setLocation.
    >>
    >> Here is my problem: I am trying to position a button on a given pixel on
    >> the screen. To do that I am using setLocation function of the JButton.
    >>
    >> Here is what I did:
    >> Created a JFrame,
    >> Created a JPanel, and added it to frame.
    >> Created a JButton, and added it to panel.
    >> Called setLocation function of the button. It is not working. The button
    >> is just created in the center of the window, where layout manager places
    >> it.
    >>
    >> What should I do to get it work?
    >>
    >> I would really appreaciate it if you could help me with my problem.
    >> Sincerely yours.
    >>
    >>

    >
    > From the docs:
    >
    > JPanel
    >
    > public JPanel()
    >
    > Creates a new JPanel with a double buffer and a flow layout.
    >
    > So it puts the button where it wants to. Just create the JPanel with a
    > null layout manager. Then you can put it anywhere you want. Of course if
    > that's all you do you won't be able to see the JButton because it doesn't
    > have a size. So call setSize(button.getPreferredSize()) to make it appear
    > correctly, more or less.
    >
    > --
    >
    > Knute Johnson
    > email s/nospam/knute/



    Hello Knute,

    I appreciate your help. It worked.

    I tried setting layout manager to null before posting here. I was unable to
    locate the button on the frame.
    Thanx for the detailed information about the size of the button.
    Sincerely.
    asd, Feb 7, 2007
    #3
  4. asd wrote:
    > "Knute Johnson" <> wrote in message
    > news:Fvbyh.251073$...
    >> asd wrote:
    >>> Hi all,
    >>>
    >>> I am doing some kind of home work using swing. Unfortunately I am having
    >>> problems with setLocation() function. I know it is a bad idea to use
    >>> setPosition and I know I should use Layout manager instead of doing that.
    >>> Still I would like to do it with setLocation.
    >>>
    >>> Here is my problem: I am trying to position a button on a given pixel on
    >>> the screen. To do that I am using setLocation function of the JButton.
    >>>
    >>> Here is what I did:
    >>> Created a JFrame,
    >>> Created a JPanel, and added it to frame.
    >>> Created a JButton, and added it to panel.
    >>> Called setLocation function of the button. It is not working. The button
    >>> is just created in the center of the window, where layout manager places
    >>> it.
    >>>
    >>> What should I do to get it work?
    >>>
    >>> I would really appreaciate it if you could help me with my problem.
    >>> Sincerely yours.
    >>>
    >>>

    >> From the docs:
    >>
    >> JPanel
    >>
    >> public JPanel()
    >>
    >> Creates a new JPanel with a double buffer and a flow layout.
    >>
    >> So it puts the button where it wants to. Just create the JPanel with a
    >> null layout manager. Then you can put it anywhere you want. Of course if
    >> that's all you do you won't be able to see the JButton because it doesn't
    >> have a size. So call setSize(button.getPreferredSize()) to make it appear
    >> correctly, more or less.
    >>
    >> --
    >>
    >> Knute Johnson
    >> email s/nospam/knute/

    >
    >
    > Hello Knute,
    >
    > I appreciate your help. It worked.
    >
    > I tried setting layout manager to null before posting here. I was unable to
    > locate the button on the frame.
    > Thanx for the detailed information about the size of the button.
    > Sincerely.
    >
    >


    Those suckers are hard to find when their size is 0x0 :).

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Feb 7, 2007
    #4
    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. Big Daddy

    Java 1.2 Swing vs. Java 1.5 Swing

    Big Daddy, Apr 15, 2005, in forum: Java
    Replies:
    2
    Views:
    499
    Pete Barrett
    Apr 16, 2005
  2. Jack

    setLocation does not work

    Jack, Nov 4, 2005, in forum: Java
    Replies:
    9
    Views:
    9,723
    Andrew Thompson
    Nov 5, 2005
  3. Jack

    setLocation (AWT)

    Jack, Nov 5, 2005, in forum: Java
    Replies:
    2
    Views:
    5,127
  4. middaycrisis
    Replies:
    0
    Views:
    1,507
    middaycrisis
    Nov 11, 2007
  5. Icarus
    Replies:
    5
    Views:
    568
    Icarus
    Aug 3, 2008
Loading...

Share This Page