Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Alignment of RadioButtons

 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
//***
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to Swing/AWT...
 
Karen Baog
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can anyone please share their expertise on this?
 
No holds barred. And no bars holed. Except this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic