Hello.
I'm using BoxLayout and I can't get components to show as I want. I
create components, do setBounds on them, make everything visible and it
dosen't work! It only works if I set bounds after I show them (make them
visible).. why? Of course I want to set where the components will be
displayed first and then display them, not display them and then move
them around... help? Below is example of the problem.
-------------------------------------------------------
import java.awt.Dimension;
import javax.swing.*;
public class TFrame extends JFrame {
JTextField tf1 = new JTextField("Test 1");
public TFrame() {
super("TEST");
setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));
setDefaultCloseOperation(EXIT_ON_CLOSE);
tf1.setBounds(50,50,200,75); // dosen't work
tf1.setMaximumSize(new Dimension(200,75));
add(tf1);
setBounds(0,0,400,200);
setVisible(true);
// tf1.setBounds(175,50,200,75); // this works
}
public static void main(String[] args) {
new TFrame();
System.out.println("Tnx a lot comp.lang.java.gui! :)");
}
} // end of class
------------------------------------------------
moumita - 27 Apr 2006 14:35 GMT
instead of using BoxLayout,u can set the Layout as null.
I think it will work..
Kova - 27 Apr 2006 14:46 GMT
> instead of using BoxLayout,u can set the Layout as null.
> I think it will work..
no, dosen't work. I choose BoxLayout becouse it (should) respects
component's proparties. Was worth a shot.
--
Kova
moumita - 27 Apr 2006 15:02 GMT
try this...
import java.awt.Dimension;
import javax.swing.*;
public class TFrame extends JFrame {
JTextField tf1 = new JTextField("Test 1");
public TFrame() {
super("TEST");
getContentPane().setLayout(null);//(new
BoxLayout(getContentPane(), BoxLayout.X_AXIS));
setDefaultCloseOperation(EXIT_ON_CLOSE);
/***Working now****/ tf1.setBounds(50,50,200,75); // dosen't
work
tf1.setMaximumSize(new Dimension(200,75));
getContentPane().add(tf1);
setBounds(0,0,400,200);
setVisible(true);
// tf1.setBounds(175,50,200,75); // this works
}
public static void main(String[] args) {
new TFrame();
System.out.println("Tnx a lot comp.lang.java.gui! :)");
}
} // end of class
it's working...:o)
Kova - 27 Apr 2006 16:07 GMT
> try this...
> it's working...:o)
yeah strangely enough this works as expected in this example, but when I
tested it, it dosen't work on my actual application :( I think I'll
listen Thomas and try to do the whole layout properly.
Tnx again.
--
Kova
Thomas Weidenfeller - 27 Apr 2006 14:52 GMT
> I'm using BoxLayout and I can't get components to show as I want. I
> create components, do setBounds on them, make everything visible and it
> dosen't work!
When you use layout managers you should not use setBounds() or
setSize(). You leave the calculation of the sizes and positions to the
layout manager.
If things don't look as you like, you have
- Chosen the wrong combination of layout managers, and/or
- Not configured one or more of the used layout managers as desired.
Work through
http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html
from start to end. Twice.
/Thomas
PS: If you get the "great hint" to use no layout manager or a null
layout manager, ignore it. Layout managers are an essential part of
AWT/Swing GUIs and you e.g. badly need them to compensate for
cross-platform differences, dynamic resizing, internationalization, etc.
Time spent to learn layout managers is time well spent.

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
Kova - 27 Apr 2006 16:12 GMT
thank you for your advice, I'll try to configure layout manager to do
what I need.
--
Kova