I'm trying to create a MouseListener
. When I hover a JButton
, I want it to change its background color and the next JButton
s in the array. For example, when I hover JButton[0][0]
, it changes the background of JButton[0][0]
, JButton[1][0]
, JButton[2][0]
and so on.
Here is how I create the JButton
array:
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
btn[i][j] = new JButton();
btn[i][j].addMouseListener(this);
btn[i][j].setBackground(Color.black);
panel.add(btn[i][j]);
}
}
And its MouseListener
:
@Override
public void mouseEntered(MouseEvent me) {
JButton event = (JButton) me.getSource();
int i = 0;
int j = 0;
btn[i][j] = event;
btn[i][j].setBackground(Color.blue);
}
@Override
public void mouseExited(MouseEvent me) {
JButton event = (JButton) me.getSource();
int i = 0;
int j = 0;
btn[i][j] = event;
btn[i][j].setBackground(Color.black);
}
I have tried doing btn[i+1][j].setBackground(Color.black);
and it sets blue [1][0]
, [2][0]
... but not [i+1][j]
.
There are no errors when I run my program.
The picture above shows what I'm trying to do.