Column-oriented Layout for Component in JScrollPan

2019-02-19 12:28发布

问题:

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.

回答1:

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



回答2:

Also take a look at SpringLayout.



回答3:

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.