title: Editing JLabel text from another class
body: <p>So what I am trying to do is to edit text of my JLabel from another class. I do it with label.setText("bla bla"); but it doesn't affect my JLabel.</p>
  
  <p>My code in GUI class lookes something like this:</p>
  
  <pre><code>public class GUI {
  
      JFrame f1 = new JFrame("GUI");
      JLabel l1 = new JLabel("Output");
      JTextField tf1 = new JTextField("");
  
      public run(){  // main calls this method.
  
      Listener listener = new Listener();
  
      f1.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
      f1.setBounds(450, 170, 400, 400);
      f1.setVisible(true);
      f1.setResizable(false);
      f1.setLayout(null);
  
      l1.setBounds(8, 8, 200, 30);
      listener.listen(tf1);
  
      f1.add(l1);
      }
  }
  </code></pre>
  
  <p>and then i have this listener class that is supposed to make changes to JLabel depending on user input. Here is the code:</p>
  
  <pre><code>public class Listener {
  
      GUI gui = new GUI();
  
      public void listen(final JTextField textfield) {
  
          textfield.getDocument().addDocumentListener(new DocumentListener() {
  
              public void changedUpdate(DocumentEvent e) {
                  test();
              }
  
              public void removeUpdate(DocumentEvent e) {
                  test();
              }
  
              public void insertUpdate(DocumentEvent e) {
                  test();
              }
  
              public void test() {
                  if (gui.everythingOK()) { // everythingOK is just a boolean that test if input is valid number and returns true if it is.
                      gui.l1.setText("No errors."); // this code is supposed to change JLabels text.
                  } else {
                      gui.l1.setText("Error."); // this code is supposed to change JLabels text.
                  }
              }
          });
      }
  }
  </code></pre>
  
  <p>Method everythingOK works perfectly, you just have to trust me on that one. It works if I define JLabel as static, but it only works first time. After first change no more changes appear in JLabel, so defining it static isn't helping me. I hope there is someone who knows what is wrong with this code. And don't mind if there is obvious mistakes, because I only took the most important parts of very very long code. </p>
  
  <p>Thanks in advance.</p>
  
  <p>Here is my everythingOK code:</p>
  
  <pre><code>public boolean everythingOK() {
          if (hasInt(tf1) &amp;&amp; isValid(tf1)) {
              return true;
          } else {
              return false;
          }
      }
  
      public boolean hasInt(JTextField textfield) {
          try {
              Integer.parseInt(textfield.getText());
              return true;
          } catch (NumberFormatException e) {
              return false;
          }
      }
  
      public boolean isValid(JTextField textfield) {
          if (hasInt(textfield)) {
              if (Integer.parseInt(textfield.getText()) &gt;= minValue &amp;&amp; Integer.parseInt(textfield.getText()) &lt;= maxValue) {
                  return true;
              } else {
                  return false;
              }
  
          } else {
              return false;
          }
  
      }
  </code></pre>