views:

274

answers:

6

I have to build a rather large form with many controls. The controls are divided in basic controls/settings and extended controls/settings. The user can decide if he wants to see only the basic or both basic and extended controls.

I've dropped all extended controls onto their own JPanel so that I can easily switch between the two views by showing or hiding this panel.

Currently I'm using GroupLayout and what happens is that the controls on different panels are not aligned:

Label aaa:     Text field
Label a:       Text field
Label aaaaaa:  Text field
----------------------------
Label b:    Text field
Label bbb:  Text field
Label bb:   Text field

Unfortunatly I found now way to "synchronize" the layouts of the two panels (except using AbsoluteLayout and fixed control coordinates)

  • Is there any way to achive this?
  • Is my whole design flawed?

EDIT: If it is possible I would like to keep the GroupLayout manager.

A: 

You could use GridLayout instead of GroupLayout which will give you uniform spacing between the columns

MrWiggles
A: 
Markus Lausberg
+1  A: 

Probably the easiest (good) way to do it is to add all the components to the main panel. Set the subpanels to non-opaque, and add the also to the main panel. The main panel the needs optimised drawing to be switched off.

Another technique is to add a spacer component. To the bottom panel add a component in the same column as the labels which dynamically takes the width component of its various size methods from the top labels. Do the same in reverse to the top panel.

Tom Hawtin - tackline
+2  A: 

As far as I know, no Swing LayoutManager (from JRE or open source) can span several panels.

I am currently working on such a feature (which I called "layouts synchronization") for my DesignGridLayout project, but it is not something easy to implements (I have started about 2 weeks ago and I still don't see exactly if and when I will get to something interesting, but I still have high hope for it;-))

One option you could check would be to add all components to the same panel (with just one GroupLayout then) and hide/show them based on user's selection. Hopefully, GroupLayout will adapt the size to the situation (after calling pack()).

If GroupLayout behaves well, then it would just be a matter of calling pack() each time after user changes his selection to show/hide extended fields.

Else you would have to manually set the size of your panel every time the user changes his selection.

jfpoilpret
+1  A: 

I think there is no way to do it with the standard layout managers. You'll probably have to write your own layout manager, but it shouldn't be too hard if you subclass GroupLayout.

Nick Fortescue
A: 

If you want to keep them in separate panels with separate layouts:

Iterate over all of the labels that you add, and find the maximum preferred width of each. Iterate a second time, and set the preferred size to that each label's preferred height, but the maximum width.

John Gardner