views:

26

answers:

2

We have a JPanel wich contains multiple JPanels wich contain JComponents (let's say JLabels and JTextboxes)

Inside each of the internal JPanels we use JGoodies Layout in order to ensure proper alignement of all Labels.

But of course we would like to have all the Labels aligned independently on which subpanel they are.

How could we do that, without fixing the width of the column which contains the JLabels?

We can't loose the JPanels since we have to have Borders around groups of Components

+3  A: 

There's no simple way to do that which I'm aware of. Your options:

  • Write your own layout manager (or extend an existing one) with that capability
  • Fixed column widths
  • Decide that panels that are visually separated by borders don't need to have their contents aligned after all
Michael Borgwardt
+1. I agree with Michael, you have to sit down and design the layout given its complexity.
Bragboy
I've tried that myself and it is incredibly difficult. The main problem, as far as I remember, is the order in which of the 2 (or more) panels layout methods are called, or even the fact that one may be called but not the other one...
jfpoilpret
I'd rather recommend using only one panel and use some "builder classes" to add components to it, so that you have re-usability of builders for several kinds of "groups of components".
jfpoilpret
+2  A: 

Just because the JPanels have borders, doesn't mean that they actually need to contain their apparent contents. Set the panels to transparent. Add the panels and the components to the enclosing panel. Add spacer components to mimic the insets of the panels in the layout. You will also need to switch off "optimised drawing", or some such, for overlapping components.

Tom Hawtin - tackline
Hm, that might work - but I suspect that it will lead to a lot of headaches in all kinds of corner cases when the borders don't match the components they're supposed to surround.
Michael Borgwardt
@Michael How do you mean? Works for me!
Tom Hawtin - tackline