I'm trying to get java to perform one action after one click and then another when the same button is pressed again, regardless of how long between clicks. Is this possible? Here's what I've tried so far:
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount()%2 == 1) {
System.out.println("You have clicked once");
}
if(e.getClickCount()%2 == 0){
System.out.println("You have clicked twice");
}
}
});
This is not what I'm after because there seems to be some timer on the button to make it more like a double click. So I'm obviously using the wrong method or something but I don't know what other options there are. I want it not to matter how quickly you click the mouse again after the first click. Hope that makes sense. Any help would be greatly appreciated, thank you.
You can do it by using a flag. Something like:
private boolean isFirstClick = true;
// ...
button.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
if(isFirstClick)
{
System.out.println("You have clicked once");
isFirstClick = false;
}
else
{
System.out.println("You have clicked twice");
isFirstClick = true;
}
}
});