Keypad Panel

The example below shows how to use actions and key bindings to implement a simple numeric keypad in Java. The digits entered are displayed as they are typed. The numeric buttons are disabled if the number of digits typed exceeds an arbitrary limit—seven in the picture shown. The Clear action simply clears the entered text and re-enables all the buttons. The Enter action examines the finished entry and forwards the action to Clear. A third action, Click, enables the numeric keys to emulate clicking the number buttons. Note how a single action may be bound to more than one key—in this case, the numbers on the keyboard or keypad. Addition details may be found in the article Keyboard Bindings in Swing. The article Key Bindings is also very helpful; it includes more examples and a convenient utility.

Copyright:

©2011, John B. Matthews. Distribution permitted without warranty under the terms of the LGPL.

Last updated 27-Jun-2011