• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

get parent object/component of a mouseListener

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a two dimensional array of jpanels arranged on a jframe. There are a lot of panels, so they are added to the array in a for loop. As they are added, each is assigned a mouseListener. When clicked, each JPanel is SUPPOSED to print out its unique ID number (assigned in the for loop).
I can't figure out how to make the mouseListener access its component to get the ID. The components are dynamic, and not static...
Here is what I have...



Thanks in advance
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use the getSource() method to get a reference to the source of the event.
 
Eric Crockett
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have actually tried that, on my print command.

System.out.println(getSource());

I expected a memory address, but I got a cannot resolve symbol. What class is this method from?
 
Eric Crockett
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found the getSource() in eventObject, but mouseListener extends eventListener, which isn't really an extension of anything. So how do I use the getSource when it is not inherited in mouseListener?
 
Eric Crockett
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it!
You have to use the MouseEvent event which is an extension of EventObject. Thanks for the help.

For those who were wondering how to do this:

for JPanel square
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will find the getSource() method on the MouseEvent object that is passed into the listener method.

But my personally preferred way of doing this is using a non-anonymous listener class, which gets the id in its constructor:

square.addMouseListener(new SquareMouseListener(ID));
 
reply
    Bookmark Topic Watch Topic
  • New Topic