GUI Objects keep moving

Hello,

I'm a bit new to Java and really new to creating GUI in Java. I've successfully create a frame with a panel with some buttons and a label. The thing is when I use frame.pack() it moves the buttons and label. I've looked in a debugger and there was this list of method's calls:

java.awt.Window.pack -->

java.awt.Container.validate -->

java.awt.Container.validateTree -->

java.awt.Container.validateTree -->

java.awt.Container.validateTree -->

java.awt.Container.validateTree -->

java.awt.Container.validateTree -->

java.awt.Container.doLayout -->

java.awt.Container.layout -->

java.awt.FlowLayout.layoutContainer -->

java.awt.FlowLayout.moveComponents

which finally calls to JButton.setLocation...

Sorry for the the mess made by all these method's calls by I hope it would make sense to any of you.

Also, this happens when changing the label but there is a bit different order of calls, something with java.awt.EventDispatchThread...

Thank you for your help :-)

[1106 byte] By [BananushkAa] at [2007-10-3 0:41:02]
# 1
You should do the pack() when you have added all the componenets and before you do the setVisible(true).
sabre150a at 2007-7-14 17:35:17 > top of Java-index,Java Essentials,New To Java...
# 2
It is after I added all the components and before I set the visibility 'true'.Anyway, the problem is not in the pack method because it happens also when I change a component...Message was edited by: BananushkA
BananushkAa at 2007-7-14 17:35:17 > top of Java-index,Java Essentials,New To Java...
# 3

when you say pack() moves them, are you setting the component's location

using setLocation() or setBounds().

if so, the layoutManager for the component's container has to be set to null

panel.setLayout(null)

but a null layout leads to a different set of problems, and is rarely recommended.

Better to use the appropriate layoutManager (or nested layoutManagers) to achieve

the desired effect.

Michael_Dunna at 2007-7-14 17:35:18 > top of Java-index,Java Essentials,New To Java...
# 4
I set the location of the GUI objects with setLocation. I'll try to see about the layout manager. Thanks.
BananushkAa at 2007-7-14 17:35:18 > top of Java-index,Java Essentials,New To Java...
# 5
I don't really get the hang of this layout managers... Can I build one? Why is the frame uses FlowLayout?
BananushkAa at 2007-7-14 17:35:18 > top of Java-index,Java Essentials,New To Java...
# 6

> I don't really get the hang of this layout managers... Can I build one?

yes

> Why is the frame uses FlowLayout?

only if you set it so - the default is a BorderLayout

default for JPanel is FlowLayout

this link might help

http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html

Michael_Dunna at 2007-7-14 17:35:18 > top of Java-index,Java Essentials,New To Java...
# 7
Well, I found the problem. I set the layout manager of the panel to null and just used setBounds instead of setLocation, now it's working wonderful! Thanks a lot for all of you for your help.
BananushkAa at 2007-7-14 17:35:18 > top of Java-index,Java Essentials,New To Java...