title: Accessing one button from another button's mouseClicked method?
body: <p>So I'm a little confused as to how I would access other on-screen components/variables (I'm not sure what to call them, but things like text boxes, buttons etc) from another component's mouseClicked method. For example, let's say that once one button had been clicked, another was disabled. I mean, in this simple calculator example (with two text boxes and an add and subtract button) it is completely pointless, but I'm just trying to understand the concept. With other programming languages, you can simply access them directly but with java, I'm not sure...anyway, here are the relevant bits of code:</p>
  
  <pre><code>     JButton btnAdd = new JButton("Add");
       btnAdd.addMouseListener(new MouseAdapter() {
          @Override
          public void mouseClicked(MouseEvent arg0) {
              int num1 = Integer.parseInt(textField.getText().toString());
              int num2 = Integer.parseInt(textField_1.getText().toString());
              int total = num1 + num2;
              btnSubtract.setEnabled(false);
              JOptionPane.showMessageDialog(null, total); 
          }
      });
  
      JButton btnSubtract = new JButton("Subtract");
      btnSubtract.setBounds(144, 124, 89, 23);
      frame.getContentPane().add(btnSubtract);
     }
   }
  </code></pre>
  
  <p>Or if I need to edit something else, here is the all the auto-generated code as well:</p>
  
  <pre><code>public class SimpleCalcApp {
  
  private JFrame frame;
  private JTextField textField;
  private JTextField textField_1;
  
  /**
   * Launch the application.
   */
  public static void main(String[] args) {
      EventQueue.invokeLater(new Runnable() {
          public void run() {
              try {
                  SimpleCalcApp window = new SimpleCalcApp();
                  window.frame.setVisible(true);
              } catch (Exception e) {
                  e.printStackTrace();
              }
          }
      });
  }
  
  /**
   * Create the application.
   */
  public SimpleCalcApp() {
      initialize();
  }
  
  /**
   * Initialize the contents of the frame.
   */
  private void initialize() {
      frame = new JFrame();
      frame.setBounds(100, 100, 450, 300);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().setLayout(null);
  
      textField = new JTextField();
      textField.setBounds(147, 27, 86, 20);
      frame.getContentPane().add(textField);
      textField.setColumns(10);
  
      textField_1 = new JTextField();
      textField_1.setBounds(147, 59, 86, 20);
      frame.getContentPane().add(textField_1);
      textField_1.setColumns(10);
  
      JButton btnAdd = new JButton("Add");
      btnAdd.addMouseListener(new MouseAdapter() {
          @Override
          public void mouseClicked(MouseEvent arg0) {
              int num1 = Integer.parseInt(textField.getText().toString());
              int num2 = Integer.parseInt(textField_1.getText().toString());
              int total = num1 + num2;
              btnSubtract.setEnabled(false);
              JOptionPane.showMessageDialog(null, total); 
          }
      });
      btnAdd.setBounds(144, 90, 89, 23);
      frame.getContentPane().add(btnAdd);
  
      JButton btnSubtract = new JButton("Subtract");
      btnSubtract.setBounds(144, 124, 89, 23);
      frame.getContentPane().add(btnSubtract);
  }
  }
  </code></pre>
  
  <p>I'm hoping this will just be a simple fix, but who knows, Java seems pretty abstract for a newbie like me. Anyway, thanks in advanced for any answers guys :)</p>