by John D. Mitchell

Java Tip 4: Customized double-clicking

news
Apr 1, 19963 mins
Core JavaDeveloperJava

What to do when the default handling of a double-click event isn't good enough

This week we do a little window washing by illustrating an alternative method to handling double-click mouse events.

Double-click mouse events are โ€œsynthetic.โ€ They are really a fiction created by the system/application. The basic idea is that if you click a mouse button twice in succession in the same location fast enough then a double-click event will be created. That begs the question of how we define โ€œsame locationโ€ and โ€œfast enough.โ€

Unfortunately, the โ€œnormalโ€ AWT behavior for double-click handling seems to be hard-coded into the Java AWT implementation. The AWT definitions for โ€œsame locationโ€ and โ€œfast enoughโ€ are also not very user friendly for folks who are a bit slow on mouse buttons or for people who have shaky hands.

The DoubleClicker applet illustrates handling mouse clicks in the applet itself. This allows the applet to not only provide its own definitions for the double-click behavior but make its behavior customizable by the user.

Alas, you need a Java-enable browser to view this applet.

You should see a nice little greeting message. Move your mouse over the running applet and click once on any of the mouse buttons. Without moving the mouse further, look down to your browserโ€™s status line and you should see some mouse event time data from the application. Click slowly and you can watch the event times change.

The basic idea behind this method of mouse-event handling is to have the applet keep track of the time of the previous event. The applet has a โ€œthresholdโ€ value to determine the maximal separation between the two click events in time. If the clicks are closer than the threshold (300 in the given code) then the double-click behavior is performed. Otherwise, just the single-click behavior takes place.

Now, start clicking in the applet again and slowly increase your clicking speed on the applet until you get the applet to display a different salutation. You should note that the event time difference displayed in the status line is indeed below the threshold value.

There are a couple of oddities in the code to note:

  • Java mandates twoโ€™s complement arithmetic and that all of the arithmetic types are signed. This applet is one case where this bites us in the behind. The mouseUp() method must use the absolute value of the difference between the two event times because the event time given by the underlying system may be built out of an unsigned number! So, after climbing nicely through the positive numbers, the event time field will seem to jump to a huge negative number. It could also be built out of some wacko system-specific bitfield information, and we may end up with the same problem. Yuck.

  • Donโ€™t forget about resetting the saved event time when thereโ€™s a double-click. This forces the single- versus double-click processing to restart afresh. This means that a triple-click will have the applet end up as if the mouse was clicked a single time. Try changing the applet as specified in the source comments and play around with multiple clicks to see which behavior you prefer.