title: Trying to get one mouse listener action to stop before another one begins
body: <p>Not sure how well I will explain this; I'm quite new to programming...</p>
  
  <p>So I'm trying to make a desktop application that draws musical notes of a given type on some sheet music when the user selects the button corresponding to that type of note. Currently, if the user selects the "Whole Note" button, the user can then start clicking on the screen and the note will be drawn where the click occurred. It will also make a "ding" sound and write some info about that note to a text file. </p>
  
  <p>That's all well and good, but unfortunately when the user selects a new button, say the "Quarter Note" button, for each mouse click there will be two notes drawn (one whole, one quarter), two dings, and two packets of info written to the file. I have no idea how to make this work! Currently, I'm trying to use threads, such that each button creates a new thread and the thread currently in use is interrupted when a new button is pressed, but that doesn't resolve the issue. </p>
  
  <p>Initially, an empty linked list of threads ("NoteThreads") is constructed. Also, a private class known as SheetMusicPane (given the variable name "smp") is constructed in order to draw the sheet music. The buttons are added in the main constructor (public CompositionStudio), whereas the method containing the mouse listener (see what follows) is contained in the SheetMusicPane private class. Not sure whether that is part of the problem.</p>
  
  <p>I have a button action listener:</p>
  
  <pre><code>button.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
          if (!threads.isEmpty()) {
              NoteThread oldThread = threads.remove();
              oldThread.interrupt();
          }
          NoteThread newThread = new NoteThread(e.getActionCommand());
          threads.add(newThread);
          newThread.run();
      }
  });
  </code></pre>
  
  <p>that produces a thread:</p>
  
  <pre><code>private class NoteThread extends Thread {
  
      private String note;
  
      public NoteThread(String note) {
          this.note = note;
      }
  
      public void run() {
          smp.getShape(smp.getGraphics(), note);
      }
  }
  </code></pre>
  
  <p>that when, on running, calls this method with graphics and a mouse listener:</p>
  
  <pre><code>public void getShape(final Graphics g, final String note) {
      this.addMouseListener(new MouseListener() {
          public void mousePressed(MouseEvent e) {
          }
          public void mouseReleased(MouseEvent e) {
          }
          public void mouseEntered(MouseEvent e) {
          }
          public void mouseExited(MouseEvent e) {
          }
          public void mouseClicked(MouseEvent e) {
             Point p = MouseInfo.getPointerInfo().getLocation();
             addShape(g, p.x, p.y, note);
             int pitch = 12;
             piano.playNote(pitch);
             advance(1.0, piano);
             try { addToFile(pitch, note);}
             catch(FileNotFoundException fnfe) {}
             catch(IOException ioe) {}
          }
       });
    }
  </code></pre>
  
  <p>The above method is responsible for drawing the note ("addShape()"), making the ding sound, and writing to the file.</p>
  
  <p>Thanks in advance for any help you can give! </p>