• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why must all Components in a Container have the same alignment?

 
Ranch Hand
Posts: 133
1
Mac OS X
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a JPanel with vertical BoxLayout. It contains four components. I set the JPanel to LEFT_ALIGNMENT, which has no effect on its components. I set the first component to LEFT_ALIGNMENT, which has no effect. Only after I have set all four components to LEFT_ALIGNMENT do any of them align properly.

This suggests that it is impossible to have varying alignments in a container. They must all be the same alignment.

I accept that this is just the way things are: "Java works in mysterious ways." And I'm sure that it is possible to work around this limitation by stacking boxes that themselves have different internal alignments.

But I still wonder what in the world was going on in the minds of the Java developers. Is there a rational reason for this oddity?

This raises my most serious criticism of Swing: the hidden gotcha. Swing is a tangled mess of cross-connecting requirements that are impossible to divine by simple inspection of the documentation. If you want to use, say, a JRadioButton, it's not enough to study the documentation on JRadioButtons; you must also consult lots of documents for which there is no obvious connection to JRadioButton other than it being part of Swing.

Of course, once you know everything there is to know about Swing, then it all makes perfect sense. And how does one learn everything there is to know about Swing? By working with it. But working with Swing doesn't reveal the hidden gotchas. Thus, the only way to learn about Swing is to spend a great deal of time studying the entire system in a purely theoretical approach, not daring to actually write code until you have the whole thing under your belt.

This is not good educational practice.

OK, I've gotten that off my chest.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I never knew they have to use the same alignment.
You do get peculiar results in box layout if you use different alignments; it is described in the Java Tutorials.
 
Chris Crawford
Ranch Hand
Posts: 133
1
Mac OS X
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the link; I had read the BoxLayout tutorial but somehow the lengthy discussion of the various alignment considerations went right over my head. Even after re-reading it carefully, I *still* don't understand why they require identical alignments from all components. I'm going to do some experimentation to see just what the limits are.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need to use identical alignment for each component but it is advisable as you don't necessarily get the results you might expect. The key sentence in the Box Layout Features section of that tutorial ie:

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.


The following examples then show what can happen when using different alignments and different maximum sizes.

I have to admit the alignment mechanism does seem to act in a rather strange manner, no doubt there are circumstances in which this behaviour is optimal but I can't think any.
 
Check your pockets for water buffalo. You might need to use this tiny ad until locate a water buffalo:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic