A SpinField Data-Entry Component
By Carl Dreher
A common data-entry component available in most modern windowing environments, but missing in Java, is the spin-button field. A spin-button field lets the user enter data into a single text field and also increment or decrement the value with up/down buttons; see
Figure 1. In this article, I'll construct a SpinField object, and along the way, show you how to build a Java interface and create a couple of other useful buttons for your thermos of Java tricks.
The Wish List
The first step in any coding exercise is to write down some specifications. This spec doesn't have to be overly formal. I simply start by writing down what I want the code to do:
- The
SpinField has button-like behavior.
- The up/down buttons can have images for labels.
- The button has "type-o-matic" characteristics.
- The value in the text field increments/decrements when an up/down button is pushed.
The first wish implies that the new SpinField can be dropped into an applet just like a standard button object, and that it will communicate with the rest of the software in the same manner (through events). "Button-like" also means that it can receive focus.
The images-for-labels wish means we can create buttons with icons.