Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Alignment of RadioButtons

 
Karen Baog
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
paneltop.add(panelRadio);

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?
//***
 
Jeff Jetton
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Swing/AWT...
 
Karen Baog
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can anyone please share their expertise on this?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic