title: How to communicate with GUI from another class
body: <p>I have problem with understanding how to communicate with GUI from another class.</p>
  
  <p>We have one class, whitch creates Frame:</p>
  
  <pre><code>public class TestFrame extends javax.swing.JFrame {
  
      javax.swing.JLabel label;
  
      public TestFrame() {
          initComponents();
          label=sampleLabel;
      }
  
      private void initComponents() {
  
          sampleLabel = new javax.swing.JLabel();
  
          setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
          getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
  
          sampleLabel.setText("anotherText");
          getContentPane().add(sampleLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 60, -1, -1));
  
          pack();
      }
  
      private javax.swing.JLabel sampleLabel;
  
  }
  
  Main class instantiate CreateFrame and tryes to overwrite label text:
  
  public class Main {
  
      public static void main(String[] args) {
  
          TestFrame frame = new TestFrame();
  
          SwingUtilities.invokeLater(new Runnable() {
              @Override
              public void run() {
                  frame.setVisible(true);
              }
          });
          frame.label.setText("anotherText");
      }
  }
  </code></pre>
  
  <p>Of course, above code doesn't work. But I am new in Java and I need somehow to access label from another class (as seen in example...)</p>
  
  <p><strong>Newest edit: Above code does work :) Hope helps...</strong></p>
  
  <p>Following pictures are showing the same frame. Only thing whitch changed was JLabel text.</p>
  
  <p><img src="https://i.stack.imgur.com/Bp9bL.jpg" alt="Sample picture"></p>
  
  <p>Please help.</p>