i have problem to change JLabel color. i am using three JLabel
variables . i am putting mouse event on this JLabel
variables. i run the and both are changes color when i am entring mouse on JLabels
. i whis is that, at the time one JLabel
change the color when I am entering the mouse on JLabel
variable.
please solve this problem.
entry.addMouseListener(this);
entry.setOpaque(true);
profile.addMouseListener(this);
profile.setOpaque(true);
public void mouseClicked(MouseEvent mc)
{
}
public void mouseEntered(MouseEvent me)
{
entry.setBackground(color);
profile.setBackground(color);
}
public void mouseExited(MouseEvent me)
{
entry.setBackground(Color.white);
profile.setBackground(Color.white);
}
public void mousePressed(MouseEvent mp)
{
}
public void mouseReleased(MouseEvent mr)
{
}
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);
}
};