开发者

How to tell which item fired a mouse listener

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.


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.


You could name each JLabel according to its index using its setName method, then use the MouseEvent's getComponent method to get the originating JLabel back, use getName on it and there's your index. That would be one way, but would involve storing the index information in two places (implicitly in its placement in the array, and explicitly as the label's name), so it's pretty much begging for inconsistency to arise.

You could also search through the array for the JLabel reference you get from getComponent, but that's not so great either, especially for large arrays.


The way I usually do it is:

int i;
for (i = 0; i <max; i++)
  if (position[i] == e.getcomponent())
    break;

now position[i] is the label you are looking for.


Just know that JButtons can look any way you'd like. They can have ImageIcons and don't even have to look like buttons.


Why is the index important? You know how to get the component, so just loop through the array to get the index.

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

How does using a button solve the problem of determining the index? However, I also agree using a button is better than a label and then you would use an ActionListener instead of a MouseListener. You can make the button look like a label by using:

button.setBorderPainted( false );

Now if you use an ActionListener you can use the setActionCommand(...) method to store the index value of the button. Then in the event you use the getActionCommand(...) method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜