Reputation: 1
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.
Upvotes: 0
Views: 378
Reputation: 5372
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.
Upvotes: 0
Reputation: 110104
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"));
Upvotes: 1
Reputation: 5109
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);
Upvotes: 1
Reputation: 3426
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:
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.
Upvotes: 1
Reputation: 46
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.
Upvotes: 1
Reputation: 1
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.
Upvotes: 0
Reputation: 1562
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.
Upvotes: 1
Reputation: 1361
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.
Upvotes: 0
Reputation: 576
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)
Upvotes: 0