title: action listeners and event sources in Swing
body: <p>OK, so if I add an <code>ActionListener</code> to a GUI element, and it's the <em>only</em> element I use that <code>ActionListener</code> with, does it matter which of the following lines (a,b) I use to get the checkbox selected state?</p>
  
  <pre><code>final JCheckBox checkbox = (JCheckBox)this.buildResult.get("cbDebugTick");
  checkbox.addActionListener(new ActionListener() {
      @Override public void actionPerformed(ActionEvent event){               
              boolean bChecked =
              // (a) checkbox.isSelected();
              // (b) ((JCheckBox)event.getSource()).isSelected();
              model.setPrintDebugOn(bChecked);
          }
  });
  </code></pre>
  
  <p>It makes sense to me that if I add the <code>ActionListener</code> object to multiple GUI elements, then I should use (b).</p>
  
  <p>And in (b), is it OK to blindly cast <code>event.getSource()</code> to <code>JCheckBox</code>, since I'm the one who added the action listener, or should I program defensively and do an <code>instanceof</code> check?</p>
  
  <p><strong>note:</strong> this question is in the context of event listeners in general; kdgregory has some good points below specifically re: checkboxes which I had neglected to consider.</p>