views:

521

answers:

4

I'm doing a school project and need help with the Layout because it's doing some funky stuff.

I only need help with the south panel. The south panel is organized something like this:

           JRadioButton JLabel1 JLabel2
           JRadioButton JLabel1 JLabel2
    JLabel3--------JTextField----------JButton

I've tried the following:

  1. Set the south JPanel to a gridlayout with 3 rows
  2. Another JPanel also with a gridlayout with 3 rows added to the first row of the gridlayout.
  3. Repeat #2 with the second and third rows. The second row has 3 rows by 3 columns.
  4. Added the components in the appropriate rows/columns.

Doesn't format the way I need it to. I've tried some other techniques I can't remember. Any suggestions? Thank you. Here's a picture of what the south panel should look like: http://www.freeimagehosting.net/image.php?d14a73db5e.jpg

It starts at the "Start Date..."

+3  A: 

Create a new JPanel and use Group Layout for that.

Like this

Using group layout allows you to specify the components that will go in the horizontal group and the components that will go in the vertical group.

c1, c2, c3
c4, c5, c6
panel[ c7, c8, c9 ]

Here's how I layout the image above:

    // Layout Horizontal components 
    layout.setHorizontalGroup(
        layout.createSequentialGroup()
        .addGroup( 
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent( c1 )
            .addComponent( c4 )
        ).addGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent( c2 )
                .addComponent( c5)
        ).addGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent( c3 )
                .addComponent( c6 )
        )

    );

    // Layout vertical components 
    layout.setVerticalGroup(
        layout.createSequentialGroup()
        .addGroup( 
             layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
             .addComponent( c1 )
             .addComponent( c2 )
             .addComponent( c3 )
         ).addGroup(
             layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                 .addComponent( c4 )
                 .addComponent( c5 )
                 .addComponent( c6 ) 
        )
    );

    southPanel.add( panel ); // the start-end dates
    southPanel.add( bookIt, BorderLayout.SOUTH ); // the enter your name... 

Give it a try.

OscarRyz
That's awesome. I've never encountered that layout before, but I'm definitely going to start using it a lot.
Paul Tomblin
@Paul. Yeap, it's new since 1.6 and I had the same reaction as you. It's like a GridbagLayout without the hassle. Actually I had never had the change to use it, that's why I decided to try it here this time. :)
OscarRyz
A: 

Seems like you'd want a GridLayout with 3 columns, not 3 rows for the bottom panel there.

In the image at the link you included, the text field is wider than the JLabel or the button. You may want to try a GridBagLayout instead.

Seth
+1  A: 

What I would do for the south JPanel is make it a 1x3 grid, then add the three components of the first row into a horizontal Box and add it to the grid, then the three components of the second row into another horizontal Box and add it to the grid, then the three components of the third row into a 3x1 grid and add it to the grid. Then you can adjust the exact look you want for the first two lines by adding various glue and strut components.

I achieve most of my layouts by putting jpanels or Boxes inside of others with different layouts. This gives you pretty good control over what you want, and allows you to build it up a piece at a time.

Paul Tomblin
The problem with using "isolated" panels, is that subsequent rows doesn't align very well ( as depicted in the OP link )
OscarRyz
Well, if you need the labels in the first two rows to line up like that, you can split things up differently. I've never seen your GroupLayout before, is that new?
Paul Tomblin
A: 

Recently I stumbled upon MigLayout, I'll try using it in my next university project and I'd advice you to give it a shot also it looks really cool and simple.

Zenzen