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 4095390
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T19:53:18+00:00 2026-05-20T19:53:18+00:00

HI all, I’m trying to write a simple star rating component. I’m fairly new

  • 0

HI all,

I’m trying to write a simple star rating component. I’m fairly new to the Java language and I’m not sure if what i want to accomplish can even be done in Java. Is it possible for me to add a JLabel inside an array of JLabel, and each JLabel in the array will have a mouse event listener. Now is it possible to set it up so that when the mouse event fires on say Label[3] that i can get the index value of it?

Here is how I built my Panel

public Rating(Integer max,int size) {

JLabel  position = new JLabel[max];


    this.setLayout(new FlowLayout());
    for(int i=0; i != max;i++){
    position[i]=new JLabel(icons.getIcon("star-empty", size));
    position[i].setOpaque(true);
    position[i].addMouseListener(this);
    add(position[i]);
    }
}


@Override
public void mouseEntered(MouseEvent e) {
    JLabel a= (JLabel) e.getComponent();
    //****Have some code in here to tell me where in the position array the event came from????***
    int index = ?????
   }

Thoughts/Idea/Suggestions please.

Note I thought of using buttons, but it looks messy and would love to find a way with ImageIcons.

THanks.

  • 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-20T19:53:19+00:00Added an answer on May 20, 2026 at 7:53 pm

    Instead of using the same listener for each label like you did:

    position[i].addMouseListener(this);
    

    …you can create a special listener class that takes the index number, and allows you to find it later:

    position[i].addMouseListener(new RatingMouseListener(i));
    

    Each label will have a separate instance of the listener with a different index value. The code for the inner class would look like something like this:

    private class RatingMouseListener extends MouseAdapter {
        private final int index;
    
        public RatingMouseListener(int index) {
            this.index = index;
        }
    
        @Override
        public void mouseEntered(MouseEvent e) {
            System.out.println("Mouse entered for rating " + index);
        }
    
        @Override
        public void mouseExited(MouseEvent e) {
            System.out.println("Mouse exited for rating " + index);
        }
    }
    

    Then, you just override any method in MouseAdapter.

    Also, like other people said, you might want to use JButtons instead of JLabels because they have better support for action events. You can still give them icons.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All I'm trying to do is something fairly simple : Create a class (let's
All APIs in Hibernate are from 1.4 and are thus not using java generics.
all. I'm a very, very new programmer. My language of choice at the moment
All I want to do is return the index of the i that is
all. We're trying to get some intersect collisions working, but the problem experience is
All my code is here,quite simple,and I don't konw where it goes wrong. Person
All time am getting below error message in groovy... Could not understand whats causing
All, I have just reinstalled Java on my machine because I found I had
All, My classpath has been set to the following folder: CLASSPATH = .;C:\Program Files\Java\jdk1.6.0_21\bin;C:\Program
I want use html5's new tag to play a wav file (currently only supported

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.