Not entirely sure what you are asking... I assume that your problem is that you have two labels and when you enter the mouse into one of them you want just that label to have a red background, not both.
To do so, you can get the label that triggered the mouse event using e.getComponent()
and then set the background for that label only. Also, you might want to use setBackground(null)
to reset the background color, since the background of the underlying frame might not always be white. Finally, you can use the MouseAdapter
class instead of the MouseListener
, providing defaults (no-op) for all those other method you do not need.
MouseListener ma = new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
e.getComponent().setBackground(Color.RED);
}
public void mouseExited(MouseEvent e) {
e.getComponent().setBackground(null);
}
};