title: How to check JToggleButton state in a class from another class using IF-statement that have actionPerformed within it?
body: <blockquote>
    <p><strong>EDIT</strong> : I found my problem but still don't have a clue for why this happen, I'm still not finished Online Lectures from Professor Mehran
    Sahami (Stanford), maybe I'll find an answer if I push on on the
    lecture videos.</p>
    
    <p>The problem is I remove my other components methods before my button
    method for efficient posting space, so I should put my <code>JToggleButton</code>
    method after my main <code>JFrame</code> method for it to work, but what if my
    <em>other components</em> inherit other class too? Which method should I put first to make all of components works? That I'll found out with
    practicing java more.</p>
    
    <p>Thank you @Dan and @SebVb for answers and suggestions, sorry if this
    just a beginners mistake :)</p>
  </blockquote>
  
  <p>I am learning java for a month now and already had simple project for learning but now I have problems with <code>JToggleButton</code>, <code>ItemEvent</code>, and <code>actionPerformed</code> included in If-statement.</p>
  
  <p>I've searching for a week for examples on using <code>actionPerformed</code> within if-statement that have <code>ItemEvent</code> from another class but i can't find a same problem to produce a working result.</p>
  
  <p>I'm trying to make a window scanner that will scan only if toggle button is selected then paint <code>JPanel</code> using buffered image (repaint every 100 millisecond) and disposed it if toggle button is deselected, but I think my approach to do it is wrong. I have one main class and two sub-classes like these:</p>
  
  <p>Main class:</p>
  
  <pre><code>public class WindowScanner {
      public static void main(String[] args) {
          new Window().setVisible(true);
      }
  }
  </code></pre>
  
  <p>Window class:</p>
  
  <pre><code>class Window extends JFrame {
      static JToggleButton captureButton = new JToggleButton("CAPTURE");
  
      @SuppressWarnings("Convert2Lambda")
      public Window() {
          // JFrame looks codes
  
          /** EDIT: these components method should be written after button method
          * JPanel looks codes
          * JLabel looks codes
          * END EDIT
          */
  
          add(captureButton);
          // capture button default looks code
          ItemListener captureListener = new ItemListener(){
              @Override
              public void itemStateChanged(ItemEvent captureButtonEvent) {
                  int captureState = captureButtonEvent.getStateChange();
                  if(captureState == ItemEvent.SELECTED){
                      // capture button SELECTED looks code
                      System.out.println("capture button is selected");
                  } else if(captureState == ItemEvent.DESELECTED){
                      // capture button DESELECTED looks code
                      System.out.println("capture button is deselected");
                  }
              }
          }; captureButton.addItemListener(captureListener);
      }
  }
  </code></pre>
  
  <p>Scanner class:</p>
  
  <pre><code>public class Scanner extends Window {
  
      private static BufferedImage boardCaptured;
      static int delay = 100;
  
      protected BufferedImage boardScanned(){
          return boardCaptured;
      }
  
      @SuppressWarnings("Convert2Lambda")
      public static void Scan() {
          if (captureButton.isSelected()) {
              ActionListener taskPerformer = new ActionListener() {
                  @Override
                  public void actionPerformed(ActionEvent captureEvent) {
                      try {
                          // capturing method
                      } catch (AWTException error) {
                          // AWTException error method
                      }
                      // is this the right place to put JPanel code?
                      JPanel panel = new JPanel();
                      boardCaptured = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
                      Graphics2D graphic = boardCaptured.createGraphics();
                      panel.setSize(500,500);
                      panel.paint(graphic);
                      panel.revalidate();
                      panel.repaint();
                  }
              }; new Timer(delay, taskPerformer).start();
          } else {
              // this suppose to end capturing if capture button isSelected() == false
          }
      }
  }
  </code></pre>
  
  <p>So here is my questions:</p>
  
  <ol>
  <li>Do I really have to make Main class separated from Window class?
  What the reason?</li>
  <li>How to make  my if statement in Scan method recognize state of my
  <code>JToggleButton</code> from Window class? Is it impossible and I had a wrong
  approach to do it?</li>
  <li>In Scanner class, i can't make a get/set for my <code>actionPerformed</code>
  (Netbeans always checked it as an error), but why I can make one for
  <code>BufferdImage</code>?</li>
  <li>If I can't get question number 3 happen, how can I make If statement
  to stop capturing using <code>Timer.stop()</code>? Or am I in wrong approach again?</li>
  <li>Do my <code>JPanel</code> in Scanner class would be produced and make a viewer
  for my buffered image?</li>
  </ol>
  
  <p>P.S. I'm sorry it cramped with questions, I tried not to make multiple post, so I make single post with multiple questions. Please notice me if there's answer before, I'm honestly can't find it or had search it with wrong tags.</p>