Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a contact list program

I am coding a contact list application in eclipse using Java Swing. How can I get a simple table layout that contains just columns and rows? I don't want row or column labels.

Something like this:

first name: john
middle name: franklin
last name: doe

Where the names would be editable text boxes etc.

What's the best component to use?

I will also have buttons below the text fields. Currently I have a JFrame which is running correctly. It pulls up a window that has my menu options correct. But when I try to do this:

myFrame.setLayout(new GridLayout(6, 2)); 

I get an error. I would like to have a grid layout of two columns and 5 rows (maybe 6). I want to have a label on the left column, and a text box on the right column. then two buttons at the bottom, centered.

like image 619
user1644782 Avatar asked Oct 07 '22 18:10

user1644782


1 Answers

You'd better of breaking your fields and controls (buttons) into separate panels, this allows you to supply different layout managers for each.

I'd start with a base JPanel using a BorderLayout.

Onto this, I'd add the "fields" panel at the CENTER position and the controls (buttons) at the SOUTH position.

For the fields, I'd use a GridBagLayout, but I'm picky like that, and for the controls panel I'd probably use a FlowLayout (unless you have access to a nice ButtonLayout manager ;))

This means you could end up with something like

enter image description here

UPDATED with code sample

public class FormPanel extends JPanel {

    private JTextField fldFirstName;
    private JTextField fldMiddleName;
    private JTextField fldLastName;
    private JTextField fldDateOfBirth;
    private JTextField fldEMail;
    private JButton okButton;
    private JButton cancelButton;

    public FormPanel() {

        setLayout(new BorderLayout());
        add(createFieldsPane());
        add(createButtonsPane(), BorderLayout.SOUTH);

    }

    public JPanel createButtonsPane() {

        JPanel panel = new JPanel(new FlowLayout());
        panel.add((okButton = createButton("Ok")));
        panel.add((cancelButton = createButton("Cancel")));

        return panel;

    }

    protected JButton createButton(String text) {

        return new JButton(text);

    }

    public JPanel createFieldsPane() {

        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(2, 2, 2, 2);
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;

        panel.add(createLabel("First Name:"), gbc);
        gbc.gridy++;
        panel.add(createLabel("Middle Name:"), gbc);
        gbc.gridy++;
        panel.add(createLabel("Last Name:"), gbc);
        gbc.gridy++;
        panel.add(createLabel("Date of Birth:"), gbc);
        gbc.gridy++;
        panel.add(createLabel("EMail:"), gbc);

        gbc.gridy = 0;
        gbc.gridx++;
        gbc.weightx = 1;
        panel.add((fldFirstName = createField()), gbc);
        gbc.gridy++;
        panel.add((fldLastName = createField()), gbc);
        gbc.gridy++;
        panel.add((fldMiddleName = createField()), gbc);
        gbc.gridy++;
        panel.add((fldDateOfBirth = createField()), gbc);
        gbc.gridy++;
        panel.add((fldEMail = createField()), gbc);

        JPanel filler = new JPanel();
        filler.setOpaque(false);

        gbc.gridy++;
        gbc.weightx = 1;
        gbc.weighty = 1;
        panel.add(filler, gbc);

        return panel;

    }

    protected JLabel createLabel(String text) {

        return new JLabel(text);

    }

    protected JTextField createField() {

        JTextField field = new JTextField(12);
        return field;

    }

}
like image 77
MadProgrammer Avatar answered Oct 10 '22 02:10

MadProgrammer