Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align Swing Components across Panels

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

Inside each of the internal JPanels we use JGoodies Layout in order to ensure proper alignment 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.

like image 756
Jens Schauder Avatar asked Sep 21 '10 14:09

Jens Schauder


1 Answers

I recommend to favor flat layouts over nested ones. In a single layout alignment is easy. Avoid TitledBorders and replace them with titled separators, separators, or just white space. That helps for the vast majority of editors and forms.

But if you want to align across multiple editors or forms, the above technique fails. The JGoodies FormLayout provides two levels to address this problems, and more generally to improve layout consistency: 1) lower bounds for sizes, 2) layout variables.

With 1) you can describe layouts that ensure a minimum width across forms. For example, if you want to say that all label columns have at least a width of 100px, you can say "[100px, pref]" for the label column.

2) goes beyond approach 1). And the motivation is to extract the 100px from your many forms. In FormLayout you can setup layout variables, for example $label that you configure as "[100px, pref]" or "right:[75dlu, pref]", etc. If you use the layout variable in all your editors, these will be consistent and you have a single place where you can configure all label columns for all editors.

like image 118
Karsten Lentzsch Avatar answered Sep 19 '22 15:09

Karsten Lentzsch