Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Chris Crawford
Ranch Hand
Posts: 116
1
Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 55681
161
  • Mark post as helpful
  • send pies
  • 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: 116
1
Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!