Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5937093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:29:27+00:00 2026-05-22T15:29:27+00:00

So I have a mouse listener that is attached to multiple objects as so:

  • 0

So I have a mouse listener that is attached to multiple objects as so:

for (int i = 0; i < Grids.size(); i++) {
     Grids.get(i).addMouseListener(new GameMouseListener()); 
   }

Now the problem I have is I need to know which of the Objects activated the handler

obviously this wont work since the var “i” is not defined inside the class and was only used in the previous for loop.
how to I know using the Handler Which Specific Object has been clicked on.

 public class GameMouseListener implements MouseListener {

    @Override
    public void mouseClicked(MouseEvent e) {
        if (Grid.get(i).isSelected()) {

            Grid.get(i).unselected();
        } else {

            Grid.get(i).selected();
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:29:28+00:00Added an answer on May 22, 2026 at 3:29 pm

    You can use e.getSource() to get the source of the event.

    Also, consider using ActionListener when possible (if the user might select with the keyboard).

    And one more thing – it the listener is generic, you might create only one instance of it instead of new instance for each component:

    GameMouseListener listener = new GameMouseListener();
    for (int i = 0; i < Grids.size(); i++) {
         Grids.get(i).addMouseListener(listener); 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that simulates mouse click. Code is something like this: [DllImport(user32.dll,
I have the following CSS that hides the mouse cursor for anything on the
Ok so i have a character_mc that moves on mouse click, a logtxt dyanamic
Writing a code that does the following: Adds a mouse listener to a web-page
I have a sortable list with a mouseleave event listener that is behaving incorrectly.
So I have a movieclip in my flash that has an event listener that
I have a mouse listener. It has some code to respond to mouseUp and
I have more than one Event Listener that calls a specific function, and I
I am writing a game in Java, and need to have mouse interaction. I
I have set mouse events on a Canvas as follows: <Canvas MouseUp=CanvasUp MouseDown=CanvasDown MouseMove=CanvasMove>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.