I was wondering if it is possible to check if a JButton is double clicked using an event Listener instead of a mouse listener. Consider the following code;
public void actionPerformed(ActionEvent arg0){
if (arg0.getClickCount() == 2){
System.out.println("You Doubled clicked");
}
}
I get an error saying getClickCount() is undefined for the type ActionEvent
. Is the click or doubleclick of a mouse not also considered as an event? Thoughts.
You want to use a MouseAdapter
. It permits you to don't clutter your code with unecessary methods (mouseDragged
, mouseEntered
etc).
public class MyClass extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
// Double click
} else {
// Simple click
}
}
}
Alternatively, if your class already extends another class, try this code:
public class MyClass extends MyBaseClass {
private MouseAdapter ma;
public MyClass () {
final MyClass that = this;
ma = new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
that.myMouseClickedHandler(e);
}
};
}
public void myMouseClickedHandler(MouseEvent e) {
if (e.getClickCount() == 2) {
// Double click
} else {
// Simple click
}
}
}