tags:

views:

152

answers:

9

I am trying to code a very simple form using java but i am running into more trouble than i expected because of the way swing lays out components, I have tried various different layout managers and still cannot layout the form the way i would like.

Here is how i would like the form to look: link text

Does anyone have any ideas about how to go about this because im stumped?

Thanks.

A: 

A form like that should be very easy to design and code by hand. Just keep reading the documentation and playing around.

Another alternative is to use a GUI Builder, Netbeans has one that really impressed me when I took a look at it last year. I don't work in Java anymore so I can't provide you with much detail.

Netbeans GUI Builder (Formerly Matisse)

Aaron
A: 

I would recommed you to use NetBeans. It's much easier since it generates code with layout manager included, so you don't have to worry about it. It's good for beginners.

nandokakimoto
+1  A: 

The easiest for your purposes is the GridLayout. As Aaron said, Netbeans has a great GUI builder if you don't want to learn the layout managers.

Chris Lacasse
A: 

I'd do a main Panel with a BorderLayout.

The south area would hold a panel with the button in it.

The center area would hold a panel with a grid layout, grid bag layout or some kind of box layout that would lay your labels and text boxes out.

AnotherCoward
+1  A: 

Short of using gridbaglayout, it is doable by borderlayout, gridlayout and flowlayout. Start by breaking the form into big visual pieces and make sub layouts under top level ones.

For example, in this case I would do this:

  1. Divide the form in two vertical pieces by gridlayout (or borderlayout); top (or central) part for the labels and textfields, bottom (or south) for the button.
  2. In top part, make either flowlayout with center align or another gridlayout to arrange the labels and textfields.
  3. In the bottom part, use flowlayout with center align for the button.

I have worked with Java swing for 4 years and it was a hurdle to design layouts at first, but it gets easier the more you practise.

Joy Dutta
It seems that 4 flowlayouts will be needed, but that seems like the obvious approach to me.
James Black
+1  A: 

Take a look at Table Layout. It is much easier to use than nearly all of the default layout managers. Your form would be very simple to construct with it.

Shaun
Since it looks like you want a layout similar to a table a table layout would be simpler to use but as Noel said the gridbag layout is very flexible and if you can suffer through learning to use it it's a great layout manager.
ChadNC
+1  A: 

GridBagLayout can be daunting at first, but it's very flexible, and is worth getting to know.

JButton button = ...;

JLabel[] labels = new JLabel[]  {
    new JLabel("Label 1"),
    ...
};

JTextField[] fields = new JTextField[] {
    new JTextField(15),
    ...
};

JPanel[] rows = new JPanel[] {
    new JPanel(),
    ...
}

Container container = getContentPane(); 
container.setLayout(new GridBagLayout());
GridBagConstraints cons = new GridBagConstraints();

// Layout each row's contents(label + text field)
cons.fill = GridBagConstraints.BOTH;
cons.insets = new Insets(2, 8, 2, 8);
for (int i = 0; i < rows.length; ++i) {
    rows[i].setLayout(new GridBagLayout());
    // Labels get 30% of the row's extra horizontal space.
    cons.weightx = 0.3;
    rows[i].add(labels[i], cons); 

    // Text fields gets 70% of the row's extra horizontal space.
    cons.weightx = 0.7;
    rows[i].add(fields[i], cons);

     // Add each row to the panel, top to bottom
     // Each row takes up all of the horizontal space.
    cons.gridx = 0;
    cons.weightx = 1;
    container.add(rows[i], cons);
    cons.gridx = GridBagConstraints.RELATIVE;
}

// Add the button at the bottom.
// Dont let button get any extra horizontal space (so it won't stretch)
cons.gridx = 0;
cons.weightx = 0;
cons.fill = GridBagConstraints.NONE;
container.add(button, cons);
Noel Ang
A: 

By far the easiest to use layout manager I've tried, at least for doing something similar to that, is DesignGridLayout. With that, the code would look something like:

layout.row().grid(new JLabel("JLabel1")).add(new JTextField());
layout.row().grid(new JLabel("JLabel2")).add(new JTextField());
...
layout.row().grid(new JLabel("JLabel5")).add(new JTextField());
layout.row().center().add(new JButton("JButton"));
ColinD
A: 

I really like the JGoodies Forms tool for laying out simple (and complex) forms. It's easy to work with and has great examples. If you want something really nice looking, you can use one of their look and feels too.

clartaq