title: How can I get the value out of a run method?
body: <p>I have two frames. The first has a button. By clicking it a second frame is opened and a timer starts. The second frame can be closed by pressing the button again or by waiting until the timer has incremented a local variable <code>x</code> to three. I had several approaches.</p>
  
  <ol>
  <li><p>The button started only the timer and within the run method of the timer, the second frame was opened. When the timer incremented the variable <code>x</code> to three the second frame closed. It worked properly. The problem was that I wasn't able to close the frame by pressing the button again. So I tried the following.</p></li>
  <li><p>The button opens the second frame and the second frame contains an object of the timer. Now I can close the second frame by clicking on the button again. But it won't close when the timer has incremented <code>x</code> to three because the second frame is now out of the run method and has no more access to the local variable <code>x</code>. </p></li>
  </ol>
  
  <p>And here is my problem. I just don't know how to get the value of <code>x</code> out of the run method.  The run method allows no parameters to pass the value. To access it with <code>timerClass.x</code> doesn't change anything. Here the code.</p>
  
  <pre><code>public class Main {
  
  public static void main(String[] args) {
  
      FrameOne frameOne = new FrameOne ();
  }}
  </code></pre>
  
  <p>.</p>
  
  <pre><code>///////////////////////////////////////     THE FIRST FRAME 
  
  public class FrameOne extends JFrame implements ActionListener {
  
  private FrameTwo frameTwo;
  private JButton btn= new JButton();
  
  ///////////////////////    PROPERTIES OF THE FIRST FRAME
  
  FrameOne(){
  
      btn.addActionListener(this);
      add(btn);
      setSize(400,400);
      setLocation(300, 250);
      setLayout(new FlowLayout());
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      setVisible(true);
  }
  
  
  ///////////////////////     OPENS THE SECOND FRAME BY CLICKING THE BUTTON
  
  public void actionPerformed(ActionEvent e) {                
  
      if (e.getSource() == btn &amp;&amp; frameTwo == null) {
  
              frameTwo = new FrameTwo();  
  }
  
  
  ///////////////////////     CLOSES THE SECOND FRAME BY CLICKING THE BUTTON AGAIN
  
  
      else if (e.getSource() == btn &amp;&amp; frameTwo != null) {  
  
              frameTwo.dispatchEvent(new WindowEvent(frameTwo, WindowEvent.WINDOW_CLOSING));
              frameTwo = null;
      }}
  
  }
  </code></pre>
  
  <p>.</p>
  
  <pre><code>///////////////////////////////////////     THE SECOND FRAME
  
  
  public class FrameTwo extends JDialog {
  
  TimerClass timerClass;
  
  FrameTwo() {
  
  
      setSize(400,400);
      setLocation(900, 250);
      setDefaultCloseOperation(DISPOSE_ON_CLOSE);
      setVisible(true);
      timerClass = new TimerClass();  // starts the timer
  
  
  //////////////////////////     HERE I TRY TO LET THE TIMER CLOSE THE FRAME AT 3    ////////
  
      if(timerClass.x == 3) {this.dispatchEvent(
          new WindowEvent(
              this, WindowEvent.WINDOW_CLOSING));
                 }}}
  </code></pre>
  
  <p>.</p>
  
  <pre><code>//////////////////       THE TIMER       
  //////////////////       IT INCREMENTS x
  //////////////////       WHEN x = 3 IT SHALL CLOSE THE FRAME 
  
  
  public class TimerClass implements EventListener{
  
              public Timer timer = new Timer(true);
              public int x;
  
      TimerClass(){
  
          timer.scheduleAtFixedRate(new TimerTask() {
              @Override
              public void run() {
                  x++;
          }
  
      },0,1000);
  }}
  </code></pre>
  
  <p>I was also recommended to use <code>frameTwo.dispose();</code> instead of the <code>this.dispatchEvent</code> command. Or use <code>frameTwo.dispatchEvent</code> and so on. Nothing ever worked. I know that it is because I am not familiar enough with passing values of variables. Please don't give me a hind to use tutorials. I need to know it from this example to make quick progress. It is my way of learning. Thank you for your sympathy and your efforts.</p>