setLocation does not work

J

Jack

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
}
}
 
A

Andrew Thompson

Jack said:
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
 
B

Benji

Jack said:
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
 
A

Andrew Thompson

- 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. ]
 
A

Andrew Thompson

Benji said:
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).
 
B

Benji

Andrew said:
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?
 
D

Daniel Dyer

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.
 
A

Andrew Thompson

Benji said:
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. ;-) ]
 
J

Jack

yeah concentrate on solving my code problem

Daniel Dyer said:
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.
 

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,755
Messages
2,569,536
Members
45,017
Latest member
GreenAcreCBDGummiesReview

Latest Threads

Top