File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Alignment of RadioButtons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Alignment of RadioButtons" Watch "Alignment of RadioButtons" New topic

Alignment of RadioButtons

Karen Baog
Ranch Hand

Joined: Mar 02, 2005
Posts: 120
Hi all,

Please shed light on this one:

If I add the following code...
panelTop.setLayout(new BoxLayout(panelBox, BoxLayout.Y_AXIS));

...the radio buttons left-aligns.
Does using BoxLayout defaults to left-align?

If I changed the 'second to the last' line (and not set the panelTop layout to BoxLayout), instead of:

change it to:
panelTop.setLayout(new BorderLayout());
panelTop.add(panelRadio, BorderLayout.CENTER);

The effect is that the radio buttons left-aligns (even though it's CENTER).

Why does CENTER still align it to left?

amerzil co-ed student<br />"Praise be the Code"
Jeff Jetton
Ranch Hand

Joined: Mar 29, 2005
Posts: 71
I can't shed a lot of light. Just a little.

According to this article: "One big difference between BoxLayout and many earlier layout managers is that BoxLayout respects each component's maximum size and X/Y alignment."

My guess is that JRadioButtons have a default alignment of left, at the component level. A BoxLayout won't override that. If you want your radio buttons to be centered (although I have no idea why you'd want that), you'd have to use the setAlignmentY() method of your button instances.

The CENTER option used when adding to a BorderLayout simply dictates what "region" of the layout to put the component in--not how to align that component when it's in that region. Regions expand to fill up empty space, so if you don't have anything in your WEST or EAST regions, it will probably behave pretty much like a BoxLayout.

Okay, so why did they come out centered in the first version of your code? Not sure. Maybe because you never set up a layout manager for the panelTop JPanel? That will give it the default FlowLayout, which, from what I gather, does not respect the components' own alignments.

That's a little unexpected, if you ask me. I wouldn't thought that the alignment in the layou the buttons were in would've taken precedence over the alignment in the layout of the "parent" container.

I'm curious to see what sort of other answers this question gets.

- Jeff
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Moving to Swing/AWT...

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Karen Baog
Ranch Hand

Joined: Mar 02, 2005
Posts: 120

Can anyone please share their expertise on this?
I agree. Here's the link:
subject: Alignment of RadioButtons
It's not a secret anymore!