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
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