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 the recent VisualSVN Server posts made me want to check it out. I
All I want is to be able to change the color of a bullet
All I want is to update an ListViewItem's text whithout seeing any flickering. This
All too often I want a WPF slider that behaves like the System.Windows.Forms.TrackBar of
All, I am trying to ensure that a file I have open with BufferedReader
All, I'm working on a java webapp that we deploy in the Resin web
All i need is a simple explanation on how does this function work I
All the STL containers that implement resize use copies to populate the new elements
I want use html5's new tag to play a wav file (currently only supported
all, I want to draw a rectangular or a container with 20 slots inside.

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.