Question

I am trying to create a GUI using JPanels that have a GridLayout. The fromPanel works perfectly, but the toPanel will only add the JTextFields. The code for the panels is almost exactly the same so I'm not sure why one works but the other doesn't. I have tried changing either rows or columns to 0, but the JLabels still don't show up in the toPanel.

Here is my code:

public class Driver extends JFrame{
private int WIDTH = 800, HEIGHT = 500, WIDTH2 = 350;
private JPanel toPanel, fromPanel, sizePanel, messagePanel, deliveryPanel,
        totalPanel, bottomPanel;
private JLabel firstLabel, lastLabel, streetLabel, cityLabel, stateLabel, zipLabel;
private JTextField toFirstText, toLastText, toStreetText, toCityText, toStateText, toZipText,
    fromFirstText, fromLastText, fromStreetText, fromCityText, fromStateText, fromZipText;

public Driver(){
    setTitle("JoAnn's Floral");
    setSize(WIDTH, HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout());
    //labels
    lastLabel = new JLabel("Last Name", JLabel.RIGHT);
    firstLabel = new JLabel("First Name", JLabel.RIGHT);
    streetLabel = new JLabel("Street", JLabel.RIGHT);
    cityLabel = new JLabel("City", JLabel.RIGHT);
    stateLabel = new JLabel("State", JLabel.RIGHT);
    zipLabel = new JLabel("ZIP", JLabel.RIGHT);
    buildToPanel();
    add(toPanel);
    buildFromPanel();
    add(fromPanel);
}

public void buildToPanel(){
    toPanel = new JPanel(new GridLayout(6, 2, 5, 5));
    toPanel.setBorder(BorderFactory.createTitledBorder("To"));
    toPanel.setPreferredSize(new Dimension(WIDTH2, HEIGHT/3));
    //text fields
    toLastText = new JTextField(10);
    toFirstText = new JTextField(10);
    toStreetText = new JTextField(10);
    toCityText = new JTextField(10);
    toStateText = new JTextField(10);
    toZipText = new JTextField(10);
    //add to layout
    toPanel.add(firstLabel);
    toPanel.add(toFirstText);       
    toPanel.add(lastLabel);
    toPanel.add(toLastText);
    toPanel.add(streetLabel);
    toPanel.add(toStreetText);
    toPanel.add(cityLabel);
    toPanel.add(toCityText);
    toPanel.add(stateLabel);
    toPanel.add(toStateText);
    toPanel.add(zipLabel);
    toPanel.add(toZipText);
}

public void buildFromPanel(){
    fromPanel = new JPanel(new GridLayout(6, 2, 5, 5));
    fromPanel.setBorder(BorderFactory.createTitledBorder("From"));
    fromPanel.setPreferredSize(new Dimension(WIDTH2, HEIGHT/3));
    //text fields
    fromFirstText = new JTextField(10);
    fromLastText = new JTextField(10);
    fromStreetText = new JTextField(10);
    fromCityText = new JTextField(10);
    fromStateText = new JTextField(10);
    fromZipText = new JTextField(10);
    //add to layout
    fromPanel.add(firstLabel);
    fromPanel.add(fromFirstText);
    fromPanel.add(lastLabel);
    fromPanel.add(fromLastText);
    fromPanel.add(streetLabel);
    fromPanel.add(fromStreetText);
    fromPanel.add(cityLabel);
    fromPanel.add(fromCityText);
    fromPanel.add(stateLabel);
    fromPanel.add(fromStateText);
    fromPanel.add(zipLabel);
    fromPanel.add(fromZipText);
}

public static void main(String[] args) {
Driver drive = new Driver();
drive.setVisible(true);

}
}
Was it helpful?

Solution

A JComponent can only appear in one container at a time. Since there is only one instance of each label, the code will only show one on-screen.

Tips

  1. See Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? (Yes.)
  2. Also consider GroupLayout for each detail panel as seen in this answer.

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