JTextField not showing properly in GridBagLayout
The gui I am working on consists of a series of JLabels and JTextField. When I run the gui, all of the objects display in the right places, but for some reason, the JTextFields are only appearing as small lines. when you type something into the box, the text filed expands to fit what is typed. In my previous (albeit limited) experience with GridBagLayouts, the JTextField came out in the proper size, and I am at a lost to explain why this one is acting differently from the others. Again, this particular gui is still a work in progess.
Code:
import javax.swing.*;
import java.awt.*;
public class recMod {
JTextField [] recData = new JTextField[8];
JButton [] recButtons = new JButton[3];
public recMod() {
JLabel [] recLabel = new JLabel[8];
JFrame frame = new JFrame("Record Modify");
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
recLabel[0] = new JLabel("First Name:");
recData[0] = new JTextField("");
addItem(panel, recLabel[0], 0, 0, 1, 1, GridBagConstraints.WEST);
addItem(panel, recData[0], 1, 0, 1, 1, GridBagConstraints.CENTER);
recLabel[1] = new JLabel("Last Name:");
recData[1] = new JTextField("");
addItem(panel, recLabel[1], 3, 0, 1, 1, GridBagConstraints.CENTER);
addItem(panel, recData[1], 4, 0, 1, 1, GridBagConstraints.EAST);
recLabel[2] = new JLabel("Vendor:");
recData[2] = new JTextField("");
addItem(panel, recLabel[2], 0, 1, 1, 1, GridBagConstraints.WEST);
addItem(panel, recData[2], 1, 1, 1, 1, GridBagConstraints.CENTER);
recLabel[3] = new JLabel("Vendor Location Code:");
recData[3] = new JTextField("");
addItem(panel, recLabel[3], 3, 1, 1, 1, GridBagConstraints.CENTER);
addItem(panel, recData[3], 4, 1, 1, 1, GridBagConstraints.WEST);
recLabel[4] = new JLabel("user Email Address:");
recData[4] = new JTextField("");
addItem(panel, recLabel[4], 0, 2, 1, 1, GridBagConstraints.WEST);
addItem(panel, recData[4], 1, 2, 2, 1, GridBagConstraints.WEST);
recLabel[5] = new JLabel("Username:");
recData[5] = new JTextField("");
addItem(panel, recLabel[5], 0, 3, 1, 1, GridBagConstraints.WEST);
addItem(panel, recData[5], 1, 3, 1, 1, GridBagConstraints.CENTER);
recLabel[6] = new JLabel("Password:");
recData[6] = new JTextField("");
addItem(panel, recLabel[6], 3, 3, 1, 1, GridBagConstraints.CENTER);
addItem(panel, recData[6], 4, 3, 1, 1, GridBagConstraints.WEST);
recLabel[7] = new JLabel("user code:");
recData[7] = new JTextField("");
addItem(panel, recLabel[7], 0, 4, 1, 1, GridBagConstraints.WEST);
addItem(panel, recData[7], 1, 4, 1, 1, GridBagConstraints.WEST);
frame.add(panel);
frame.setVisible(true);
frame.setSize(500,400);
}
private void addItem(JPanel p, JComponent c, int x, int y,
int width, int height, int align) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = width;
gbc.gridheight = height;
gbc.weightx = 0.0;
gbc.weighty = 0.0;
gbc.insets = new Insets(5,5,5,5);
gbc.anchor = align;
gbc.fill = GridBagConstraints.HORIZONTAL;
p.add(c, gbc);
}
public static void main(String args[]) {
new recMod();
}
}