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

  • Home
  • SEARCH
  • 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

How can i seperately get x,y coordinates and events of multiple mouse attached to
I have a mouse hook that (for an unknown reason) dies every now and
I have a new web app that is packaged as a WAR as part
I have a mouse listener. It has some code to respond to mouseUp and
I have a div that appears when the user's mouse leaves the document with
I have a sortable list with a mouseleave event listener that is behaving incorrectly.
Writing a code that does the following: Adds a mouse listener to a web-page
I have a small gallery of thumbnails. When I place my mouse pointer over
I have a server dropdownlist in an Ajax updatepanel. When I use the mouse
Im looing for a way to have a simple on mouse over-effect. (I want

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.