2

I'm working on my first Swing application and I'm having a layout problem concerning a dialog window I created for my users to enter values in certain fields. Because the number of fields displayed varies based on a user choice, I use a JScrollPane in my dialog, setting its viewport to a panel to which I add my field components.

For every field to be displayed, I create and add three components:

  1. "field name" label
  2. Field component (usually a JTextField, but it also could be a JComboBox or a JDateChooser control)
  3. "field type" label

i.e.

namelabel: |____| (String)

name2label: |__| (Number)

All three of these components can be of varying lengths, so my challenge has been to find a tidy way to layout these components. What I've been doing is setting the layout manager for the main pane to be a BoxLayout that uses the y-axis (i.e. it lays out components vertically). I then create a pane for each field, set the layout manager for that pane then add all three field components to that pane. I've tried both a FlowLayout and BoxLayout for the individual panes, and I've had issues with both of those layout managers.

I set the FlowLayout manager to use a left justification, but due to the varying lengths of the components, this led to a crooked-column layout. I set the BoxLayout to use the X-axis (i.e. lay things out horizontally) but the consequent centering of the components resulted in a vast spacing between each component. And prior to using individual panes, I tried to use GridLayout but I was never able to get it to honour my three-column requirement, causing the fields to be split across rows. I also looked briefly at an article about the GroupLayout manager but it seemed intimidating :)

Does anyone have any suggestions on how to layout a varying number of rows of three components of varying length within a JScrollPane in a neat, compact way? Thanks in advance...

Sheldon R.

Sheldon R.
  • 452
  • 2
  • 7
  • 26
  • 1
    You have to use Grid Bag Layout - example is [Here](http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html) – Ravindra Gullapalli Feb 02 '12 at 23:17
  • How are the components supposed to be laid out? Can you provide some ASCII art of some rows with an 'expanded' view shown? Why not include the type after the field name in the same label? – Andrew Thompson Feb 03 '12 at 00:04
  • Andrew, I edited my question to do an ASCII rendition of what I'm trying to do. Including the type after the field name is an option, I suppose, but I'm trying to emulate the look-and-feel of our main application that our users are already familiar with. Plus, wouldn't I still have the same issue just with one less column of components? – Sheldon R. Feb 03 '12 at 15:35

3 Answers3

3

It's a common problem: MiGLayout is a good choice. Alternatively, BoxLayout is illustrated here, and Group Layout is shown here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks for the suggestions, guys. One other idea I had that I didn't mention was to use three vertical panes, one for each type of component. I would add these panes to the main one using a FlowLayout, then use a Y-axis-oriented BoxLayout to add components to these panes. I'll report back when I've settled on an approach that does what I want it to, but thanks again to all for giving me an idea of what my options are... – Sheldon R. Feb 03 '12 at 14:56
1

Also take a look at SpringLayout.

texclayton
  • 189
  • 1
  • 4
1

Update: My three-column idea didn't work out for the same reason most of my other ideas didn't work i.e. BoxLayout, like most of the layout managers, tends to expand the component to fill as much space as it can, so my fields were being rendered as enormous :)

So I bit the bullet and tried to figure out GroupLayout, based on the example shown by @trashgod being similar to what I was trying to achieve. After figuring out how to do what I wanted in the GroupLayout way, I initially ran into the same expanding-field issue. Then the Oracle GroupLayout tutorial showed me how to keep the components from being resized i.e. using the four-argument version of the addcomponent method: addComponent(field, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) I tried that and it worked like a charm. Thanks again, @trashcan for pointing me in the right direction, and thanks to everyone else who chimed in with ideas...

Sheldon R.

Sheldon R.
  • 452
  • 2
  • 7
  • 26