Search code examples
javaswinggridbaglayout

JComboBox changes JTextField width after inserting in GridBagLayout


I am trying to add 3 combo boxes for a person's date of birth: one for month, then day, then year. When I add all 3 combo boxes, it changes the width of the JTextFields. Any help on how to resolve this issue would be greatly appreciated.

Also is there a better way to add in the months for the JComboBox, instead of inserting it one by one?

Here is the code:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class Home extends JFrame {

    private JButton btnClear, btnNext;
    private JTextField txtName, txtAddress, txtCity, txtState,
            txtZipCode, txtPhoneNumber, txtEmail;
    private JComboBox txtDate, txtYear, txtMonth;
    private JLabel labelName, labelAddress, labelCity, labelState,
            labelZipCode, labelPhoneNumber, labelEmail, labelDOB;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Home().setVisible(true);
            }
        });
    }

    public Home() {
        createView();

        setTitle("Job Hiring Systems");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(600, 500);
        setMinimumSize(new Dimension(600, 500));
        //pack();
        setLocationRelativeTo(null);
        setResizable(false);
    }

    private void createView() {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        //panel.setBorder(new EmptyBorder(25,10,25,10));
        getContentPane().add(panel);

        /*
                 * North Panel
         */
        JPanel northPanel = new JPanel();
        northPanel.setBackground(Color.GRAY);
        panel.add(northPanel, BorderLayout.NORTH);

        JLabel labelWelcomeMessage = new JLabel("Welcome to the Job Hiring Systems");
        labelWelcomeMessage.setFont(new Font("Serif", Font.BOLD, 25));
        northPanel.add(labelWelcomeMessage, BorderLayout.CENTER);
        /*
                 * End of North Panel
         */

 /*
                 * Center Panel
         */
        JPanel centerPanel = new JPanel();
        centerPanel.setBackground(Color.yellow);
        centerPanel.setSize(300, 300);
        GridBagLayout layout = new GridBagLayout();

        centerPanel.setLayout(layout);
        GridBagConstraints gbc = new GridBagConstraints();

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 0;
        labelName = new JLabel("Enter your name: ");
        labelName.setFont(new Font("Serif", Font.PLAIN, 15));
        centerPanel.add(labelName, gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        txtName = new JTextField(25);
        txtName.setHorizontalAlignment(JTextField.CENTER);
        txtName.setHorizontalAlignment(SwingConstants.LEFT);
        centerPanel.add(txtName, gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 1;
        labelAddress = new JLabel("Enter your address: ");
        labelAddress.setFont(new Font("Serif", Font.PLAIN, 15));
        centerPanel.add(labelAddress, gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        txtAddress = new JTextField(25);
        txtAddress.setHorizontalAlignment(JTextField.CENTER);
        txtAddress.setHorizontalAlignment(SwingConstants.LEFT);
        centerPanel.add(txtAddress, gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 2;
        labelCity = new JLabel("Enter your city: ");
        labelCity.setFont(new Font("Serif", Font.PLAIN, 15));
        centerPanel.add(labelCity, gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        txtCity = new JTextField(25);
        txtCity.setHorizontalAlignment(JTextField.CENTER);
        txtCity.setHorizontalAlignment(SwingConstants.LEFT);
        centerPanel.add(txtCity, gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 3;
        labelState = new JLabel("Enter your State: ");
        labelState.setFont(new Font("Serif", Font.PLAIN, 15));
        centerPanel.add(labelState, gbc);

        gbc.gridx = 1;
        gbc.gridy = 3;
        txtState = new JTextField(25);
        txtState.setHorizontalAlignment(JTextField.CENTER);
        txtState.setHorizontalAlignment(SwingConstants.LEFT);
        centerPanel.add(txtState, gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 4;
        labelZipCode = new JLabel("Enter your Zip Code: ");
        labelZipCode.setFont(new Font("Serif", Font.PLAIN, 15));
        centerPanel.add(labelZipCode, gbc);

        gbc.gridx = 1;
        gbc.gridy = 4;
        txtZipCode = new JTextField(25);
        txtZipCode.setHorizontalAlignment(JTextField.CENTER);
        txtZipCode.setHorizontalAlignment(SwingConstants.LEFT);
        centerPanel.add(txtZipCode, gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 5;
        labelPhoneNumber = new JLabel("Enter your Phone Number: ");
        labelPhoneNumber.setFont(new Font("Serif", Font.PLAIN, 15));
        centerPanel.add(labelPhoneNumber, gbc);

        gbc.gridx = 1;
        gbc.gridy = 5;
        txtPhoneNumber = new JTextField(25);
        txtPhoneNumber.setHorizontalAlignment(JTextField.CENTER);
        txtPhoneNumber.setHorizontalAlignment(SwingConstants.LEFT);
        centerPanel.add(txtPhoneNumber, gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 6;
        labelEmail = new JLabel("Enter your Email: ");
        labelEmail.setFont(new Font("Serif", Font.PLAIN, 15));
        centerPanel.add(labelEmail, gbc);

        gbc.gridx = 1;
        gbc.gridy = 6;
        txtEmail = new JTextField(25);
        txtEmail.setHorizontalAlignment(JTextField.CENTER);
        txtEmail.setHorizontalAlignment(SwingConstants.LEFT);
        centerPanel.add(txtEmail, gbc);

        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 0;
        gbc.gridy = 7;
        labelDOB = new JLabel("Enter your Date of Birth: ");
        labelDOB.setFont(new Font("Serif", Font.PLAIN, 15));
        centerPanel.add(labelDOB, gbc);

        gbc.gridx = 1;
        txtMonth = new JComboBox();
        txtMonth.addItem("January");
        txtMonth.addItem("February");
        txtMonth.addItem("March");
        txtMonth.addItem("April");
        txtMonth.addItem("May");
        txtMonth.addItem("June");
        txtMonth.addItem("July");
        txtMonth.addItem("August");
        txtMonth.addItem("September");
        txtMonth.addItem("October");
        txtMonth.addItem("November");
        txtMonth.addItem("December");
        centerPanel.add(txtMonth, gbc);

        gbc.gridx = 2;
        txtDate = new JComboBox();
        for (int i = 0; i < 31; i++) {
            txtDate.addItem(i);
        }
        centerPanel.add(txtDate, gbc);

        gbc.gridx = 3;
        txtYear = new JComboBox();
        for (int i = 1940; i < 2017; i++) {
            txtYear.addItem(i);
        }
        centerPanel.add(txtYear, gbc);
        panel.add(centerPanel);

        /*
                 * End of Center Panel
         */

 /*
                 * South Panel
         */
        JPanel southPanel = new JPanel();
        southPanel.setBackground(Color.GREEN);
        panel.add(southPanel, BorderLayout.SOUTH);

        btnClear = new JButton("CLEAR");
        southPanel.add(btnClear, BorderLayout.CENTER);

        btnNext = new JButton("NEXT");
        southPanel.add(btnNext, BorderLayout.WEST);
        /*
                 * End of South Panel
         */
    }
}

Solution

  • When I add all 3 combo boxes, it changes the width of the JTextFields. Any help on how to resolve this issue would be greatly appreciated.

    Each component in a GridBagLayout takes up 1 cell (one space vertically and 1 horizontally) by default. Each time you add a combo box, you increase the number of columns in the grid, but the text fields still take 1 horizontal space, aligning themselves with the first combo box.

    enter image description here

    You want the text fields to align themselves with the last combo box, or in other words, to take whatever space remains in the grid. You can do that by specifying

    gbc.gridwidth = GridBagConstraints.REMAINDER;
    

    for your text fields. (In your case gbc.gridwidth = 3 would also work, but you might need to change that if you add more components.)

    Note that since the layout manager assigns space to the components, specifying a number in the constructor of the text fields won't do much,

    Also is there a better way to add in the months for the JComboBox, instead of inserting it one by one.

    Yes, I suggest you take advantage of the new Date-Time API. There is an Enum Month that has your constants readied. Don't forget to specify the generic type for your combo boxes:

    JComboBox<Month> txtMonth = new JComboBox<>();
    for (Month month : Month.values())
        txtMonth.addItem(month);
    

    And I'll leave you do correct the capitalization yourself (hint: getDisplayName).