Input and Output with Data Entry Fields

Three types of data entry fields appear in BreezySwing: IntegerField, and DoubleField, and JTextField (actually, a built-in class in javax.swing).  As the names imply, each type of field is suited for the input and output of a specific type of data.  In the case of the numeric fields, conversion between numbers and text is handled internally within the field object.  Thus, the input method getNumber returns a number, whereas the output method setNumber expects a numeric argument, when used with the numeric fields.  The DoubleField method setPrecision is available to modify the precision used to display floating-point numbers.  The methods getText and setText are used to read and write strings with text fields.  The data entry field methods are summarized in the next table:

Field Type

Method

What it does

IntegerField,

DoubleField

getNumber() Returns (as input) the number contained in the field.

isValidNumber()

Returns true if the contents of the field represent a valid number, or false otherwise.

setNumber(number)

Outputs the number to the field.

DoubleField

setPrecision(width)

Sets the number of digits to display to right of the decimal point.

JTextField

getText()

Returns (as input) the string contained in the field.

setText(text)

Outputs the string to the field.

 

To make a data entry field read-only, you call the method setEditable(false) on the field.

Note that the input method getNumber returns 0 and resets the field's data to 0 if the user has entered a number with an invalid format (such as bad digits).  This type of error can be detected under program control, before calling getNumber, by calling the method isValidNumber on the field.

 

 

Back to tutorial Next topic: Attributes of window components