Creating field list

S

Si

I'm trying to create a panel which contains a rows of fields for contact
details. Each row has one label (i.e. field name) and one data field. The
effect I'm trying to achieve is so the field name 'column' has a static
width, and the data 'column' expands with the window (i.e. so that field
*widths* can be expanded).

My initial thoughts were to create a parent BorderLayout panel. Then add a
GridLayout panel for the labels to the WEST part of that parent panel and
another GridLayout panel to the CENTER part of the parent panel for the data
fields.

I would have thought this would produce two columns, where the second one is
resizable. For some reason however when I run the code below the second
GridLayout panel for the data appears *under* the panel for the labels.

Any ideas how I can sort this?

-----------

private JPanel contactsPane() throws MethodException{

//create panels
JPanel contacts_panel = new JPanel();
JPanel fields_panel = new JPanel();
JPanel label_grid = new JPanel(); //grid of labels for fields
JPanel field_grid = new JPanel(); //grid of labels for fields

label_grid.setBackground(Color.GREEN);
field_grid.setBackground(Color.BLUE);

fields_panel.setLayout(new BorderLayout());
label_grid.setLayout(new GridLayout(2,1)); //two rows for each label
field_grid.setLayout(new GridLayout(2,1)); //two rows for each field

GuiComponents gc = new GuiComponents();

//puts border round box
contacts_panel.setBorder(gc.makeBorder("Contacts",1,1,1,10));

//set layout
contacts_panel.setLayout(new BoxLayout(contacts_panel, BoxLayout.Y_AXIS));
fields_panel.setLayout(new BoxLayout(fields_panel, BoxLayout.Y_AXIS));

JLabel title_label = new JLabel("Title");
JTextField title = gc.makeJTextField(true, 100, 10, new Font("Arial",
Font.PLAIN, 12), Color.RED);
JLabel forename_label = new JLabel("Forename");
JTextField forename = gc.makeJTextField(true, 100, 10, new Font("Arial",
Font.PLAIN, 12), Color.RED);

label_grid.add(title_label);
label_grid.add(forename_label);
field_grid.add(title);
field_grid.add(forename);

// add label and field grids to parent panel
fields_panel.add(label_grid, BorderLayout.WEST);
fields_panel.add(field_grid, BorderLayout.EAST);

//add buttons to main panel
contacts_panel.add(fields_panel);

//return panel
return contacts_panel;

}//end of filesPanel
 
V

Vova Reznik

I found 2 problems:
1. Layout of your panel is not the BorderLayout:
fields_panel.setLayout(new BorderLayout());
fields_panel.setLayout(new BoxLayout(fields_panel, BoxLayout.Y_AXIS));
2. Add field_grid to the CENTER instead of EAST
line
fields_panel.add(field_grid, BorderLayout.EAST);
replace with
fields_panel.add(field_grid, BorderLayout.CENTER);
See
http://java.sun.com/docs/books/tutorial/uiswing/layout/border.html

I may want to use SpringLayout that is very good for kind of forms
http://java.sun.com/docs/books/tutorial/uiswing/layout/spring.html
 
N

Nigel Wade

Si said:
I'm trying to create a panel which contains a rows of fields for contact
details. Each row has one label (i.e. field name) and one data field. The
effect I'm trying to achieve is so the field name 'column' has a static
width, and the data 'column' expands with the window (i.e. so that field
*widths* can be expanded).


Use GridBagLayout. This sort of thing is what it's designed for.

The items in the second column should have the constraints fill HORIZONTAL, and
have a weightx > 0.0.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top