title: Cannot update JLabel in Swing
body: <p>Hi guys, I'm relatively new to programming (just to throw that out there) and I'm  making a simple math quiz using Swing. It's designed to ask addition, subtraction, multiplication and division problems only. I want to make it so that when the user clicks the "Check Answer" button, the ActionListener updates the Label (that contains the question), and the question itself. I managed to update the question itself, but the JLabel does not get updated at all. I tried updating the label by removing it, then using the question contructor to build a new question and label, then adding the updated label and question back. I found that I can't remove then add it back. I can only add a brand new label or remove it completely. Thanks for you help, I hope it's not too messy!</p>
  
  <pre><code>import javax.swing.*;
  import java.awt.event.*;
  import java.awt.*;
  
  public class MathQuiz extends JFrame
  {
  
      private int num1, num2, hardNum1, hardNum2, WIDTH = 400, HEIGHT = 150;
      private double answer, input;
      private int questionNum = 1, operatorSelector;
      private String userInput;
      private JLabel questionLabel;
      private JPanel question;
      private JTextField answerBox;
      private JButton enter;
      private String addition = "+", subtraction = "-", multiplication = "*", division = "/";
      private int hard = 1, easy = 0, easiness = 0;
      private int easinessSelector[] = {hard, easy};
      private int[][] selector = new int[2][2];
      private int easyNum = 0, hardNum = 1, number;
  
      String[] operator = {addition, subtraction, multiplication, division};
  
  
      /**Constructor
       * 
       */
      public MathQuiz()
      {
          setTitle("Math quiz");
          setDefaultCloseOperation(EXIT_ON_CLOSE);
          setLayout(new BorderLayout());
          answerBox = new JTextField(6);
          add(answerBox, BorderLayout.NORTH);
          questionPanelBuild();
          questionConstructor();
          enter = new JButton("Check answer");
          enter.addActionListener(new ButtonListener());
          add(enter, BorderLayout.SOUTH);
          setResizable(false);
          setSize(WIDTH, HEIGHT);
          setVisible(true);
  
      }
  
      private class ButtonListener implements ActionListener
      {
          public void actionPerformed(ActionEvent e)
          {
              userInput = answerBox.getText();
              input = Double.parseDouble(userInput);
  
              if(input == answer)
              {
                  questionNum +=1;
                  question.remove(questionLabel);
                  question.remove(question);
                  questionConstructor();
              }
              else if(input != answer)
              {
                  JOptionPane.showMessageDialog(null, "Sorry, that's incorrect.");
              }
          }
      }
  
      private void questionPanelBuild()
              {
                  question = new JPanel();
              }
  
      private void questionConstructor()
      {
          Random rand1 = new Random();
          num1 = rand1.nextInt(101);
          Random rand2 = new Random();
          num2 = rand2.nextInt(101);
          Random rand3 = new Random();
          operatorSelector = rand3.nextInt(4);
          Random rand4 = new Random();
          hardNum1 = rand4.nextInt(21);
          Random rand5 = new Random();
          hardNum2 = rand5.nextInt(21);
  
          if(operatorSelector == 0)
              answer = num1 + num2;
  
          if(operatorSelector == 1)
              answer = num1 - num2;
  
          if(operatorSelector == 2)
              answer = hardNum1 * hardNum2;
  
          if(operatorSelector == 3)
              answer = hardNum1 / hardNum2;
  
          selector[0][0] = num1;
          selector[1][0] = num2;
          selector[0][1] = hardNum1;
          selector[1][1] = hardNum2;
  
          if(operatorSelector == 0 || operatorSelector == 1)
              easiness = easinessSelector[hardNum];
          if(operatorSelector == 2 || operatorSelector == 3)
              easiness = easinessSelector[easyNum];
  
          questionLabel = new JLabel("What is: " + selector[0][easiness] + " " + operator[operatorSelector] + " " + selector[1][easiness] + "?");
          questionLabel.setBorder(BorderFactory.createTitledBorder("Question " + questionNum));
          questionLabel.setFont(new Font("Arial", Font.BOLD, 18));
          question.add(questionLabel);
          add(question, BorderLayout.CENTER);
  
      }
      public static void main(String[] args)
      {
          new MathQuiz();
      }
  }
  </code></pre>