title: How do I add a MouseListener to a graphics object inside another graphics object?
body: <p>I'm working on a GUI for a card game and am using the <a href="http://jtf.acm.org/javadoc/student/index.html" rel="nofollow noreferrer">ACM's student graphics library</a> for the sake of familiarity. I have written a program that draws my solitaire game to the screen, and am having trouble making it interactive.</p>
  
  <p><strong>Background:</strong></p>
  
  <p>There are a lot of classes here, and I'll do my best to describe them each.</p>
  
  <ul>
  <li>Top level JFrame containing the application.
  
  <ul>
  <li>GCanvas (that holds all the graphics objects)
  
  <ul>
  <li>SolitaireGameControl (GCompound holding all the other GCompounds making up the solitaire game)
  
  <ul>
  <li>Array of PileViews, a pile of cards (GCompound consisting of an array of Cards)
  
  <ul>
  <li>Cards (GCompound consisting of rectangles and labels)</li>
  </ul></li>
  </ul></li>
  </ul></li>
  </ul></li>
  </ul>
  
  <p>(<strong>GCompound:</strong> a collection of graphics objects treated as one object. (If <code>car</code> was a GCompound, it would have <code>GOval[] wheels, GRect body</code> and so when I add it to the canvas, it displays as one object))</p>
  
  <p>A card as seen from the top-level class would look like a bit like this: <code>jFrame.gCanvas.solitaireGameControl.pileViews[pile number].cardView</code></p>
  
  <p>What I've been trying to do is add a MouseListener to every single card, so that when a card is clicked and a MouseEvent is fired, MouseEvent <code>e.getSource()</code> = the card that was clicked.</p>
  
  <p>Here's how it looks now:</p>
  
  <pre><code>public SolitaireGameControl(SolitaireGame game) {
      this.game = game; // Model of the game.
      this.pileViews = PileView.getPileViews(game.drawPiles); // ArrayList of PileViews (the pile of cards)
  
      for(PileView pv : pileViews) {
          for(CardView cv : pv.cardViews) {
              cv.addMouseListener(this); // add a mouseListener to the card
          }
      }
  
      this.addMouseListener(this); // if I don't include this, nothing happens when I click anything. If I do include this, this whole object is the source.
  }
  
  @Override
  public void mouseClicked(MouseEvent e) {
      System.out.println(e.getSource()); // should return the card I clicked.
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/XJPDa.png" alt="Picture of the problem"></p>
  
  <p>When I run this program, the source of every event is SolitaireGameControl, granted I leave in the <code>this.addMouseListener(this);</code>. If I take out this statement, nothing is printed at all, leading me to believe that the mouseListeners I have added are only working one level deep. (The first GCompound on the canvas, not the GCompounds inside it.)</p>
  
  <p>Therefore, my question is as follows: Is there a way to get a MouseListener for a GCompound <strong>inside of a</strong> GCompound <strong>inside of a</strong> GCompound, and have MouseEvent's getSource to correctly identify the card? If not, is there a way to restructure my program to make it work as intended? (I know I should really be using a better graphics library for starters.)</p>