JTextField is too small/ can't seem to format it

34 Views Asked by At

I am trying to format these JTextFields, and they were working fine. Whenever I add the 4th and 5th rows however, the JTextfields become like 1 character long (like in the screenshot). I don't see any syntax errors or anything since the 1st through 3rd rows work fine on their own -- so I assume I'm just not aware of some limitation with the Layout Manager or something.

enter image description here

public PatientTab () {
        
        GridBagConstraints gbc = new GridBagConstraints();
        setLayout(new GridBagLayout());
        
        ButtonGroup sexGroup = new ButtonGroup();
                sexGroup.add(maleRadioButton);
                sexGroup.add(femaleRadioButton);
        
        //row 1
        gbc.gridx = 0;
                gbc.gridy = 0;
        add(new JLabel("First Name:"), gbc);
        gbc.gridx = 1;
        add(fName, gbc);
        
        gbc.gridx = 2;
        add(new JLabel("Middle Initial:"), gbc);
        gbc.gridx = 3;
        add(mName, gbc);
        
        gbc.gridx = 4;
        add(new JLabel("Last Name:"), gbc);
        gbc.gridx = 5;
        add(lName, gbc);
        
        // Row 2
            gbc.gridx = 0;
            gbc.gridy = 1;
            add(new JLabel("Sex:"), gbc);

            gbc.gridx = 1;
            add(maleRadioButton, gbc);

            gbc.gridx = 2;
            add(femaleRadioButton, gbc);
        
            // Row 3
            gbc.gridx = 0;
            gbc.gridy = 2;
        add(new JLabel("SSN:"), gbc);
        
        gbc.gridx = 1;
        add(ssn, gbc);
        
        gbc.gridx = 2;
        add(new JLabel("Primarary-care Doctor:"), gbc);
        
        gbc.gridx = 3;
        add(primaryDoctor, gbc);
        
        // Row 4
        gbc.gridx = 0;
            gbc.gridy = 3;
        add(new JLabel("Secondary-care Doctor:"), gbc);
        
        gbc.gridx = 1;
        secondaryDoctor.setPreferredSize(new Dimension(10,20));
        add(secondaryDoctor, gbc);
        
        
        // Row 5
        gbc.gridx = 0;
        gbc.gridy = 4;
        add(new JLabel("Current Phone Number:"), gbc);
        
        gbc.gridx = 1;
        add(currentPhone, gbc);
        
        
        /*gbc.gridx = 0;
        gbc.gridy = 1;
        add(addButton, gbc);*/
        
    }
1

There are 1 best solutions below

0
Eugene Trofimov On

sometimes it's needed to set size of frame or the panel. also method pack() could help:

 frame.pack();
 frame.setMinimumSize(frame.getSize());