Search code examples
javaswinguser-interfacejlabel

JLabel wont show up on button click


This is my first GUI so any advice is good advice here but mainly I'm just trying to work out why my errorLabel wont show up. I've tried setVisible, setOpaque, having the location set manually and finally (as you can see below) just making it its own panel to sit on and it still won't show up on button press.

Any ideas?

package GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import javax.swing.*;


/**
 *
 * @author User
 */
public class AddChair extends JFrame {

    private final int FRAME_HEIGHT = 600;
    private final int FRAME_WIDTH = 400;
    private final String ADD_CHAIR_TITLE = "Chair";
    private final int TEXT_BOX_WIDTH = FRAME_WIDTH / 4;
    private final int TEXT_BOX_HEIGHT = 25;

    private JPanel basePanel;
    private JPanel leftPanel;
    private JPanel rightPanel;
    private JPanel lowerPanel;
    private JPanel itemIDPanel;
    private JPanel woodTypePanel;
    private JPanel armrestPanel;
    private JPanel quantityPanel;
    private JPanel itemIDInputPanel;
    private JPanel woodTypeInputPanel;
    private JPanel quantityInputPanel;
    private JPanel armrestInputPanel;
    private JPanel confirmCancelPanel;
    private JPanel errorPanel;

    private JTextField itemIDTextField;
    private JTextField quantityTextField;

    private JRadioButton walnut;
    private JRadioButton oak;
    private JRadioButton yes;
    private JRadioButton no;
    
    private JButton confirmButton;
    private JButton cancelButton;

    public AddChair() {
        frameSetup();
        add(basePanel());
    }

    private void frameSetup() {   // sets up base frame
        setSize(FRAME_WIDTH, FRAME_HEIGHT);
        setVisible(true);
        setAlwaysOnTop(true);
        setTitle(ADD_CHAIR_TITLE);
        setLocationRelativeTo(null);
    }

    private JPanel basePanel() {   //sets up a base panel to arrange other panels on
        basePanel = new JPanel(new BorderLayout());
        basePanel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT));
        basePanel.add(leftPanel(), BorderLayout.WEST);
        basePanel.add(rightPanel(), BorderLayout.EAST);
        basePanel.add(lowerPanel(), BorderLayout.SOUTH);
        return basePanel;
    }

    private JPanel leftPanel() {   //sets up the left panel which will contain labels
        leftPanel = new JPanel();
        leftPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4));
        leftPanel.setVisible(true);
        labelPanels();
        labels();
        return leftPanel;
    }

    private void labelPanels() {//sets up the panels for the labels
        itemIDPanel = new JPanel();
        woodTypePanel = new JPanel();
        armrestPanel = new JPanel();
        quantityPanel = new JPanel();

        itemIDPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
        woodTypePanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
        armrestPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
        quantityPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));

        itemIDPanel.setVisible(true);
        woodTypePanel.setVisible(true);
        armrestPanel.setVisible(true);
        quantityPanel.setVisible(true);

        leftPanel.add(itemIDPanel);
        leftPanel.add(woodTypePanel);
        leftPanel.add(armrestPanel);
        leftPanel.add(quantityPanel);
    }

    private void labels() {   //sets up the and adds the labels for the user 
        JLabel itemIDLabel = new JLabel("Item ID:");
        JLabel woodTypeLabel = new JLabel("Wood Type:");
        JLabel armrestLabel = new JLabel("Armrest:");
        JLabel quantityLabel = new JLabel("Quantity:");

        itemIDLabel.setVisible(true);
        woodTypeLabel.setVisible(true);
        armrestLabel.setVisible(true);
        quantityLabel.setVisible(true);

        itemIDPanel.add(itemIDLabel);
        woodTypePanel.add(woodTypeLabel);
        armrestPanel.add(armrestLabel);
        quantityPanel.add(quantityLabel);
    }

    private JPanel rightPanel() {   // sets up the right panel which will contain the user input objects
        rightPanel = new JPanel();
        rightPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4));
        rightPanel.setVisible(true);
        userInputPanels();
        return rightPanel;
    }

    private void userInputPanels() {//sets up the panels for the labels
        itemIDInputPanel = new JPanel();
        woodTypeInputPanel = new JPanel();
        armrestInputPanel = new JPanel();
        quantityInputPanel = new JPanel();

        itemIDInputPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
        woodTypeInputPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
        armrestInputPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));
        quantityInputPanel.setPreferredSize(new Dimension(FRAME_WIDTH / 2, FRAME_HEIGHT * 3 / 4 / 5));

        itemIDInputPanel.setVisible(true);
        woodTypeInputPanel.setVisible(true);
        armrestInputPanel.setVisible(true);
        quantityInputPanel.setVisible(true);

        userInputs();

        rightPanel.add(itemIDInputPanel);
        rightPanel.add(woodTypeInputPanel);
        rightPanel.add(armrestInputPanel);
        rightPanel.add(quantityInputPanel);
    }

    private void userInputs() {   // sets up and adds to the right panel the user input objects
        itemIDTextField = new JTextField("");
        walnut = new JRadioButton("Walnut");
        oak = new JRadioButton("Oak");
        yes = new JRadioButton("Yes");
        no = new JRadioButton("No");
        quantityTextField = new JTextField("");
        
        itemIDTextField.setPreferredSize(new Dimension(TEXT_BOX_WIDTH, TEXT_BOX_HEIGHT));
        itemIDInputPanel.add(itemIDTextField);

        ButtonGroup woodTypeGroup = new ButtonGroup();
        woodTypeGroup.add(oak);
        woodTypeGroup.add(walnut);
        woodTypeInputPanel.add(walnut);
        woodTypeInputPanel.add(oak);

        ButtonGroup armrestGroup = new ButtonGroup();
        armrestGroup.add(yes);
        armrestGroup.add(no);
        armrestInputPanel.add(yes);
        armrestInputPanel.add(no);

        quantityTextField.setPreferredSize(new Dimension(TEXT_BOX_WIDTH, TEXT_BOX_HEIGHT));
        quantityInputPanel.add(quantityTextField);
    }

    private JPanel lowerPanel() {   // sets up the lower panel for confirm and cancel buttons
        lowerPanel = new JPanel(new BorderLayout());
        lowerPanel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT / 4));
        lowerPanel.setVisible(true);
        confirmCancelPanel();
        errorPanel();
        confirmCancelButtons();
        return lowerPanel;
    }
    
    private void confirmCancelPanel(){
        confirmCancelPanel = new JPanel();
        confirmCancelPanel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT / 8));
        confirmCancelPanel.setBackground(Color.blue);
        confirmCancelPanel.setVisible(true);
        lowerPanel.add(confirmCancelPanel, BorderLayout.NORTH);
    }
    
    private void errorPanel(){
        errorPanel = new JPanel();
        errorPanel.setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT / 8));
        errorPanel.setBackground(Color.yellow);
        errorPanel.setVisible(true);
        lowerPanel.add(errorPanel, BorderLayout.SOUTH);
    }
    
    private void confirmCancelButtons(){
        confirmButton = new JButton("Confirm");
        cancelButton = new JButton("Cancel");
        
        confirmButton.setVisible(true);
        cancelButton.setVisible(true);
        
        confirmCancelPanel.add(confirmButton);
        confirmCancelPanel.add(cancelButton);
        
        cancelButtonLogic();
        confirmButtonLogic();
    }
    
    private void cancelButtonLogic(){
        cancelButton.addActionListener((ActionEvent ae) -> {
            this.dispose();
        });
    }
    
    private void confirmButtonLogic(){
        confirmButton.addActionListener((ActionEvent ae) -> {
            errorMessage();
            
        });
    }
    
    private void errorMessage(){
        JLabel errorLabel = new JLabel("Error");
        errorLabel.setOpaque(true);
        errorLabel.setVisible(true);
        errorLabel.setForeground(Color.BLACK);
        errorPanel.add(errorLabel);
    }
}

Solution

  • I've tried setVisible,

    Swing components are visible by default. There is no need to invoke setVisible(true) on the components

    my errorLabel wont show up.

    When you add a component to a visible GUI the basic code is:

    errorPanel.add(errorLabel);
    errorPanel.revalidate();
    errorPanel.repaint();
    

    This will make sure the layout manager is invoked so the label is given a size/location.

    However, another solution is to create and add the "errorLabel" to the "errorPanel" when the GUI is created. You would set the set to " ". so the label takes up some space. Then you can update the label with various error messages simply by invoking error.Label.setText(...). Of course this would mean the label would need to be defined as an instance variable.