First of all, I'm new to GUI so I may have made some terrible mistakes..
right, so I have a two dimensional array of buttons and I need to identify which button in the array is clicked, hovered over, and hovered off. For now I just want my program to say:
"*click* I am button" + x + y //when a button is pressed
"*hover over* I am button" + x + y //when the mouse enters a button
"*hover off* I am button" + x + y //when the mouse exits a button
So far I’ve solved the clicking with getActionCommand()
for (int i = 0; i < buttons.length; i++){
for (int j = 0; j < buttons[0].length; j++) {
buttons[i][j].setActionCommand(i + "_" + j);
buttons[i][j].addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("*click* I am button " + e.getActionCommand());
}
});
}
}
My real problem is the hovering because, as far as I know, the getActionCommand() isn't an option. I know of getComponent() however. This is my code so far:
buttons[i][j].addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
//Print : "*hover over* I am button" + x + y
}
public void mouseExited(java.awt.event.MouseEvent evt) {
//Print : "*hover off* I am button" + x + y
}
});
My idea was to somehow save the x y coordinates of each button on the button object and then read it with getComponent().
I hope this wasn't too confusing.
Thanks in advanced.