title: updating Swing JTextField
body: <p>I can't <code>.setText(...)</code> for a <code>JTextField</code> outside of the class that creates the gui. I'm very confused and I feel like there is something basic I am missing. I need some help here. </p>
  
  <p>Here is what I am doing:</p>
  
  <p>In a class (called <code>MainClass</code>) I create an instance of a class that creates my gui </p>
  
  <pre><code>TestText gui = new TestText();
  </code></pre>
  
  <p>with a constructor that sets the default settings (a <code>JTextField</code> and a button with a listener). Then I call the a setter that I wrote, where I pass it a string that is to set the text of the <code>JTextField</code>:</p>
  
  <pre><code>gui.setText("new");
  </code></pre>
  
  <p>But <code>"new"</code> doesn't show up on the gui.</p>
  
  <p>I know my setter works from within the class because if I make a call to the setter from the button that I created in gui then the changes show up on the gui. </p>
  
  <p>The part that really confuses me is this: If I call my getter just before my setter, then it returns the old value. Then if I call the getter again after I call the setter then it returns the new value, while the gui continues to show the old value. I thought that maybe it just isn't repainting the gui so I tried all kinds of permutations of <code>.invalidate()</code>, <code>.validate()</code>, <code>.update()</code> and <code>.repaint()</code>, all from the <code>MainClass</code> and from inside the setter. But none did anything.</p>
  
  <p>Is it possible that I somehow have 2 different instances of the gui and I'm only editing one of them?</p>
  
  <pre><code>import javax.swing.*;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  
  public class TestText {
      private JTextField textField;
      private JButton button;
      private JPanel frame;
      JFrame jFrame;
      public void setText(String text) {
          textField.setText(text);
      }
      public String getText() {
          return textField.getText();
      }
      public TestText() {
          this.textField.setText("98.6");
          this.jFrame = new JFrame("TestText");
          button.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  setText("new (button)");
              }
          });
      }
      public void setData(TestText data) {
          data.setText("new (setData)");
      }
      public void getData(TestText data) {
      }
      public boolean isModified(TestText data) {
          return false;
      }
      public void createGui(String[] args) {
          jFrame.setContentPane(new TestText().frame);
          jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          jFrame.pack();
          jFrame.setVisible(true);
      }
  }
  </code></pre>
  
  <p>and then here's the main class that I'm trying to create the gui from:</p>
  
  <pre><code>public class MainClass {
  
      public static void main(String[] args) {
  
          TestText gui = new TestText();
          gui.createGui(null);
  
          System.out.println(gui.getText());
          gui.setData(gui);
          System.out.println(gui.getText());
          gui.setText("new (MainClass)");
          System.out.println(gui.getText());
      }
  
  }
  </code></pre>