Box, Boxlayout problem

S

Sean Anderson

here is the code

Box form = Box.createVerticalBox();

form.setBorder(BorderFactory.createRaisedBevelBorder());




// Put form fields here

for (int a=0; a < 6; a++){

JL[a].setAlignmentX(0); //inserted for the hell-of-it to see if it
would change anything, supposed to default left

form.add(JL[a]);


if (a==0) {form.add(Title = new JTextField(30));
Title.setMaximumSize(new Dimension (325,20)); }

if (a==1) {form.add(Author = new JTextField(25));
Author.setMaximumSize(new Dimension (300,20));}

if (a==2) {form.add(Category = new JComboBox());
Category.setEditable(true); Category.setMaximumSize(new Dimension
(300,20));}

if (a==3) {form.add(Code = new JTextField(20)); Code.setMaximumSize(new
Dimension (200,20));}

if (a==4) {form.add(Publisher = new JComboBox());
Publisher.setEditable(true); Publisher.setMaximumSize(new Dimension
(300,20));}

if (a==5) {form.add(Year = new JTextField(10)); Year.setMaximumSize(new
Dimension (100,20));}

}



when the app runs the JTextFields and JLabels are all in the center!? I
have done this in a JFrame and this worked fine as everything aligned to the
left. I have checked through several references/doc and none of them get
detailed enough. What am I doing wrong?



Sean
 
M

Menno Holscher

Sean Anderson wrote:

Box form = Box.createVerticalBox();
when the app runs the JTextFields and JLabels are all in the center!? I
have done this in a JFrame and this worked fine as everything aligned to
the
left. I have checked through several references/doc and none of them get
detailed enough. What am I doing wrong?

Nothing. A BoxLayout lays out the items in the direction you are not
lay-outing in centered. It is part of the behaviour of the manager. It
should be documented in the layout manager or under Box.
 
S

Sean Anderson

you said"A BoxLayout lays out the items in the direction you are not
lay-outing in centered."

what ?

I have used this layout before and everything aligned left when I set the
max size but this was in a JFrame contentPane
not a JPanel. this is from the BoxLayout tut:

" The X alignments affect not only the components' positions relative to
each other, but also the location of the components (as a group) within
their container. The following figures illustrate alignment of components
that have restricted maximum widths."

it goes on to show compenents aligned left (0.0, LEFT_ALIGNMENT) the way I'm
trying to align my components:

" In the first figure, all three components have an X alignment of 0.0
(Component.LEFT_ALIGNMENT). This means that the components' left sides
should be aligned. Furthermore, it means that all three components are
positioned as far left in their container as possible. "

I have set my X Alignment for most components and they are all still
centered in the Panel!! I know I'm missing something simple?!

Sean
 
S

Sean Anderson

As I see from reading closer (eyes squinted) I see that ALL components need
to have their setAlignmentX set to LEFT not just some or most. Works great
now.

Sean
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top