I have a Swing application that deals with date and time, so a lot of tests are done changing the system’s date and time settings.
During the tests, we noticed that after decreasing the clock, the first click is ignored by the application.
Is it a bug of Swing/Java/Windows? Is there a workaround to this?
Interestingly, this issue only happens when decreasing the date/time settings. If I increase it, the application behaves normally.
Situation:
- Swing application running.
- Decrease Windows date and time settings (e.g. change time from 15:00 to 14:00).
- Notice that the first click in the Swing application does not fire any action.
Code example (you can use it to testify the situation):
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
final JFrame frame = new JFrame("frame");
final JButton button = new JButton("button");
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
System.out.println("Button Pressed!");
}
});
frame.add(button);
frame.setSize(200, 200);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(final WindowEvent e) {
System.exit(0);
}
});
}
}
I’ve debugged it via Eclipse and found out what is happening.
The problem here is that the comparison made by Swing checking for multi-click is this:
Here,
currentTime - lastTimeyields a negative value. It’s less than0(mymultiClickThreshhold), so it does not fire the action event:All the source listed above is in
javax.swing.plaf.basic.BasicButtonListener.The
Buttonclass does have asetMultiClickThreshhold, but it throwsIllegalArgumentExceptionin case the threshhold is less than0.So, as a workaround, I did this: