6

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.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Jens Schauder
  • 77,657
  • 34
  • 181
  • 348

5 Answers5

7

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.

  • Hi Karsten, I agree on the flat layout, but it was a requirement (back then) to have groups :-/ I didn't know about layout variables. Good to know. – Jens Schauder Jun 01 '12 at 08:49
  • Sounds good, but how to make a titled separator in Swing? A [`JSeparator`](https://docs.oracle.com/javase/9/docs/api/javax/swing/JSeparator.html) can make an untitled one, vertical or horizontal. – Ole V.V. Jun 24 '18 at 13:18
4

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
  • 342,105
  • 78
  • 482
  • 720
  • +1. I agree with Michael, you have to sit down and design the layout given its complexity. – bragboy Sep 21 '10 at 14:34
  • 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 Sep 27 '10 at 06:06
  • 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 Sep 27 '10 at 06:07
2

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
  • 145,806
  • 30
  • 211
  • 305
1

I've read suggestions about using JSeparator rather than multiple JPanels with borders, if aligning widgets is your priority.

JSeparator gives the visual effect of grouping widgets, but themselves are just another simple widget fitting into the same JPanel. Read the tutorial for important tips such as setting the preferred size. http://download.oracle.com/javase/tutorial/uiswing/components/separator.html

Also, Apple's current guidelines suggest simply using empty space as a divider between groupings, as an alternative to borders and separators.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

This sample code can help to fix your alignment problem:

//c is instance of the content pane
c.setLayout(gl=new GridLayout(3,0));
//jp is a jpanel. I took this panel because there is no alignment
//setting for a specifiq cell of grid Layout.
jp.setAlignmentX(CENTER_ALIGNMENT);
//jl is the jLabel
jp.add(jl);
//finally I add that with the frame
c.add(jp);

Hope it will help you to solve your issue.

Alex K
  • 22,315
  • 19
  • 108
  • 236