Question

I want to make an input form in Java so that the user can enter details.

Something like this:

desired form


My code

import java.awt.GridLayout;
import javax.swing.*;

class JOptionPaneTest {

public static void main(String[] args) {
    String[] items = {"One", "Two", "Three", "Four", "Five"};
    JComboBox combo = new JComboBox(items);
    JTextField field1 = new JTextField("1234.56");
    JTextField field2 = new JTextField("9876.54");
    JPanel panel = new JPanel(new GridLayout(0, 1));
    panel.add(combo);
    panel.add(new JLabel("Field 1:"));
    panel.add(field1);
    panel.add(new JLabel("Field 2:"));
    panel.add(field2);
   int result = JOptionPane.showConfirmDialog(null, panel, "Test",
        JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
    if (result == JOptionPane.OK_OPTION) {
        System.out.println(combo.getSelectedItem()
            + " " + field1.getText()
            + " " + field2.getText());
    } else {
        System.out.println("Cancelled");
    }
}
}

My output form:

current state of form

I think I must change my layout to something like BorderLayout. Any ideas how to get the look of the form at the top of the question?

Was it helpful?

Solution

Yes, you have to change layout. Have a look at SpringLayout and this example:

alt text
(source: sun.com)

String[] labels = {"Name: ", "Fax: ", "Email: ", "Address: "};
int numPairs = labels.length;

//Create and populate the panel.
JPanel p = new JPanel(new SpringLayout());
for (int i = 0; i < numPairs; i++) {
    JLabel l = new JLabel(labels[i], JLabel.TRAILING);
    p.add(l);
    JTextField textField = new JTextField(10);
    l.setLabelFor(textField);
    p.add(textField);
}

//Lay out the panel.
SpringUtilities.makeCompactGrid(p,
                                numPairs, 2, //rows, cols
                                6, 6,        //initX, initY
                                6, 6);       //xPad, yPad

SpringLayout works fine for this simple form, but there is third party libraries that has more features. I.e. MiG Layout.

OTHER TIPS

Another way to create a form using GridBagLayout, producing the following result:

enter image description here

Code:

JPanel addressPanel = new JPanel();
Border border = addressPanel.getBorder();
Border margin = new EmptyBorder(10, 10, 10, 10);
addressPanel.setBorder(new CompoundBorder(border, margin));

GridBagLayout panelGridBagLayout = new GridBagLayout();
panelGridBagLayout.columnWidths = new int[] { 86, 86, 0 };
panelGridBagLayout.rowHeights = new int[] { 20, 20, 20, 20, 20, 0 };
panelGridBagLayout.columnWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
panelGridBagLayout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
addressPanel.setLayout(panelGridBagLayout);

addLabelAndTextField("City:", 0, addressPanel);
addLabelAndTextField("Street:", 1, addressPanel);
addLabelAndTextField("State:", 2, addressPanel);
addLabelAndTextField("Phone:", 3, addressPanel);
addLabelAndTextField("Mail:", 4, addressPanel);

The helper method addLabelAndTextField:

private void addLabelAndTextField(String labelText, int yPos, Container containingPanel) {

    JLabel label = new JLabel(labelText);
    GridBagConstraints gridBagConstraintForLabel = new GridBagConstraints();
    gridBagConstraintForLabel.fill = GridBagConstraints.BOTH;
    gridBagConstraintForLabel.insets = new Insets(0, 0, 5, 5);
    gridBagConstraintForLabel.gridx = 0;
    gridBagConstraintForLabel.gridy = yPos;
    containingPanel.add(label, gridBagConstraintForLabel);

    JTextField textField = new JTextField();
    GridBagConstraints gridBagConstraintForTextField = new GridBagConstraints();
    gridBagConstraintForTextField.fill = GridBagConstraints.BOTH;
    gridBagConstraintForTextField.insets = new Insets(0, 0, 5, 0);
    gridBagConstraintForTextField.gridx = 1;
    gridBagConstraintForTextField.gridy = yPos;
    containingPanel.add(textField, gridBagConstraintForTextField);
    textField.setColumns(10);
}

You are currently using a GridLayout, which can be fine for your need.

However, you should initialize it with the actual number of rows and columns you will need. In your case:

new GridLayout(0, 2); 

0 for rows means there is not limit, and you have 2 columns, one for the labels, and one for the input component. See the Java tutorial for more information on GridLayouts.

alt text
(source: sun.com)

Note however that the GridLayout will make all "cells" to be the same size, which can be a problem for the labels.

However, Jonas is right, a SpringLayout is probably more adapted to your need.

You can do this with DesignGridLayout. The following snippet should work (sorry I could not test it I am not on my dev station right now):

DesignGridLayout layout = new DesignGridLayout(panel);
layout.row().grid(streetAddressLabel).add(streetAddressField);
layout.row().grid(cityLabel).add(cityField);
layout.row().grid(stateLabel).add(stateSpinner);
layout.row().grid(zipLabel).add(zipField);
layout.emptyRow();
layout.row().right().add(setAddressButton, clearAddressButton);

Then you would use JDialog (rather than JOptionPane) to display your panel.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top