setLocation does not work

Discussion in 'Java' started by Jack, Nov 4, 2005.

  1. Jack

    Jack Guest

    Please compile the following code with Javac.
    Look at the objects in your browser and you will see that they dont move.
    Why?
    What can I do to reposition these objects?


    import java.awt.*;
    import java.applet.Applet;

    public class ListSimpleTest extends Applet {
    public void init() {
    List list = new List(5, false);
    list.add("Seattle");
    list.add("Washington");
    list.add("New York");
    list.add("Chicago");
    list.add("Miami");
    list.add("San Jose");
    list.add("Denver");
    add(list);

    list.setSize(400, 420);

    list.setName("This is a name.");

    int height = list.getHeight();
    int width = list.getWidth();

    TextField f1 = new TextField("1111");
    f1.setLocation(10, 10);
    add(f1);

    TextField f2 = new TextField("1111");
    f2.setLocation(20, 20);
    add(f2);

    TextField f3 = new TextField("1111");
    f3.setLocation(30, 30);
    add(f3);

    TextField f4 = new TextField("1111");
    f4.setLocation(80, 80);
    add(f4);

    f1.setText(Integer.toString(height));
    f2.setText(list.getName());

    f3.setText(Integer.toString(f2.getX()));
    f4.setText(Integer.toString(f2.getY()));

    //http://java.sun.com/developer/onlineTraining/awt/contents.html#AWTScrollbar
    }
    }
     
    Jack, Nov 4, 2005
    #1
    1. Advertising

  2. Jack wrote:

    > Please compile the following code with Javac.


    Thanks for the offer. No need.

    > Look at the objects in your browser and you will see that they dont move.
    > Why?


    You are fighting the layout manager.

    The two 'solutions' to this are as follows..
    1) use a NullLayout[1]
    2) learn to use layouts to achieve the effect you need.

    [1] 'cleverly' obscured, since that is not a solution.

    > What can I do to reposition these objects?


    Two recommendations
    - Do the Layout tutorial (part of the Swing tutorial, AFAIR)[2]
    - Post GUI questions to the group specialised for them[3], as
    you get a lot of yob c.l.j.programmers that *think* they are
    competent to answer GUI qns. (but aren't).

    [2] Both linked from the GUI FAQ, posted regularly to..
    [3] <http://www.physci.org/codes/javafaq.jsp#cljg>

    HTH
     
    Andrew Thompson, Nov 4, 2005
    #2
    1. Advertising

  3. Jack

    Benji Guest

    Jack, while high on whiteboard markers, wrote:
    > Please compile the following code with Javac.
    > Look at the objects in your browser and you will see that they dont move.
    > Why?
    > What can I do to reposition these objects?


    you have to set the layout manager to null before you can absolutely
    position objects like that.

    e.g. this.setLayout(null);

    I wouldn't use a null layout, though. Try looking up "LayoutManager
    example" on google.

    Or, since I know Roedy will do it, I'll save him the trouble. =)

    http://mindprod.com/jgloss/layoutmanager.html

    --
    Of making better designs there is no end,
    and much refactoring wearies the body.
     
    Benji, Nov 4, 2005
    #3
  4. > - Post GUI questions to the group specialised for them[3], as
    > you get a lot of yob c.l.j.programmers that *think* they are
    > competent to answer GUI qns. (but aren't).


    Oh, I see you are already ahead of me there, but..
    Please refrain fom multi-posting.
    <http://www.physci.org/codes/javafaq.jsp#xpost>

    [ Check out the tutorials I pointed you toward, or as well,
    Monique Y. M. just made a post to the (c.l.j.gui) group
    linking to two excellent layout tutorials - check them out. ]
     
    Andrew Thompson, Nov 4, 2005
    #4
  5. Benji wrote:

    > you have to set the layout manager to null..


    Aaaaargh!

    Benji, intil you understand why that is such poor advice for x-plat,
    x-version, multi-plaf GUI's on a variety of screen resolutions and font
    sizes, please refrain from giving GUI advice.

    If you really wish to hone your GUI building skills, a good group
    to read is c.l.j.gui (mentioned elsewhere in this thread, in a post
    in which I warn the OP not to take advice from c.l.j.programmers
    ...and thinking of that very advice).
     
    Andrew Thompson, Nov 4, 2005
    #5
  6. Jack

    Benji Guest

    Andrew Thompson, while high on whiteboard markers, wrote:
    > in which I warn the OP not to take advice from c.l.j.programmers
    > ..and thinking of that very advice).


    I told him that he shouldn't use a null layout, and should use layout
    managers. How exactly was that bad advice?

    --
    Of making better designs there is no end,
    and much refactoring wearies the body.
     
    Benji, Nov 4, 2005
    #6
  7. Jack

    Daniel Dyer Guest

    On Fri, 04 Nov 2005 21:23:26 -0000, Andrew Thompson
    <> wrote:

    > Benji wrote:
    >
    >> you have to set the layout manager to null..

    >
    > Aaaaargh!
    >
    > Benji, intil you understand why that is such poor advice for x-plat,
    > x-version, multi-plaf GUI's on a variety of screen resolutions and font
    > sizes, please refrain from giving GUI advice.


    To be fair, that wasn't his recommended advice, he was just explaining why
    the original poster's code didn't work and then suggested to use a layout
    manager (so we're all in agreement there).

    Dan.


    --
    Daniel Dyer
    http://www.dandyer.co.uk
     
    Daniel Dyer, Nov 4, 2005
    #7
  8. Benji wrote:

    > Andrew Thompson, while high on whiteboard markers, wrote:
    >
    >>in which I warn the OP not to take advice from c.l.j.programmers
    >>..and thinking of that very advice).

    >
    >
    > I told him that he shouldn't use a null layout, and should use layout
    > managers. How exactly was that bad advice?


    Ummm... My bad for 'going off' when I saw that first line.

    Sorry for the wasted bandwith.

    [ I must cut down on them dang whiteboard markers. ;-) ]
     
    Andrew Thompson, Nov 4, 2005
    #8
  9. Jack

    Jack Guest

    yeah concentrate on solving my code problem

    "Daniel Dyer" <> wrote in message
    news:eek:...
    > On Fri, 04 Nov 2005 21:23:26 -0000, Andrew Thompson
    > <> wrote:
    >
    >> Benji wrote:
    >>
    >>> you have to set the layout manager to null..

    >>
    >> Aaaaargh!
    >>
    >> Benji, intil you understand why that is such poor advice for x-plat,
    >> x-version, multi-plaf GUI's on a variety of screen resolutions and font
    >> sizes, please refrain from giving GUI advice.

    >
    > To be fair, that wasn't his recommended advice, he was just explaining why
    > the original poster's code didn't work and then suggested to use a layout
    > manager (so we're all in agreement there).
    >
    > Dan.
    >
    >
    > --
    > Daniel Dyer
    > http://www.dandyer.co.uk
     
    Jack, Nov 5, 2005
    #9
  10. Jack wrote:

    > yeah concentrate on solving my code problem


    yeah pay me
     
    Andrew Thompson, Nov 5, 2005
    #10
    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. Jack

    setLocation (AWT)

    Jack, Nov 5, 2005, in forum: Java
    Replies:
    2
    Views:
    5,129
  2. asd
    Replies:
    3
    Views:
    7,673
    Knute Johnson
    Feb 7, 2007
  3. middaycrisis
    Replies:
    0
    Views:
    1,512
    middaycrisis
    Nov 11, 2007
  4. Icarus
    Replies:
    5
    Views:
    573
    Icarus
    Aug 3, 2008
  5. Phi!
    Replies:
    1
    Views:
    180
Loading...

Share This Page