Panel Swing Alignment

We have a JPanel that contains several JPanels that contain JComponents (say, JLabels and JTextboxes)

Within each internal JPanels, we use JGoodies Layout to ensure that all labels are aligned correctly.

But, of course, we would like all the labels to be aligned independently of each other, on which sub-panel they are.

How can we do this without fixing the width of the column that contains JLabels?

We cannot lose JPanels, since we must have borders around groups of components

+6
java alignment layout swing jgoodies
source share
5 answers

Just because JPanel borders have borders does not mean that they really need to contain their visible content. Set the panels to transparent. Add panels and components to the cover panel. Add separator components to simulate panel inserts in the layout. You also need to disable “optimized drawing” or some of them for overlapping components.

+2
source share

I recommend supporting flat layouts over nested ones. In one layout arrangement is easy. Avoid TitledBorders and replace them with title separators, dividers, or just white space. This helps the vast majority of editors and forms.

But if you want to align across multiple editors or forms, the technique described above has failed. JGoodies FormLayout provides two levels to solve these problems and, more generally, to improve layout consistency: 1) lower bounds on dimensions, 2) layout variables.

With 1) you can describe layouts that provide minimal width in different shapes. For example, if you want to say that all label columns are at least 100 pixels wide, you can say "[100px, pref]" for the label column.

2) goes beyond the scope of approach 1). And the motivation is to extract 100px from your many forms. In FormLayout, you can configure layout variables, such as $ label, which you configure as "[100px, pref]" or "right: [75dlu, pref]", etc. If you use the layout variable in all your editors, it will be and you have one place where you can configure all the shortcut columns for all editors.

+6
source share

There is no easy way to do what I know. Your options:

  • Use this feature to write your own layout manager (or expand an existing one)
  • Fixed column widths
  • Decide that panels visually separated by borders do not have to align the content after all
+4
source share

I read suggestions for using JSeparator, rather than multiple JPanels with borders, if the priority is to build the widget.

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

In addition, Apple’s current guidelines suggest that you simply use white space as a separator between groups as an alternative to borders and separators.

+1
source share

This sample code will help fix the 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 this helps you solve your problem.

0
source share

All Articles