The DefaultFormatter Class

DefaultFormatter is a concrete implementation of AbstractFormatter that provides enough functionality for many purposes. It can be used for classes with a constructor that takes a single String. To support other classes, you may have to override DefaultFormatter’s stringToValue( ) method and its valueToString( ) method if the class’s toString( ) method is not adequate.

DefaultFormatter maintains the field’s editValid property, checking to see if the current edit is valid after every user keystroke and calling setEditValid( ) as appropriate.

Properties

Table 20-3 shows the properties defined by DefaultFormatter.

Table 20-3. DefaultFormatter properties

Property

Data type

get

is

set

Default value

allowsInvalid

boolean

·

 

·

true

commitsOnValidEdit

boolean

·

 

·

false

overwriteMode

boolean

·

 

·

true

valueClass

Class

·

 

·

null

The allowsInvalid property controls whether the field’s content may be temporarily invalid during an edit. Consider an integer field with a current value of 25 that the user wants to change to 19. The user may decide to do this by pressing the Backspace key twice, then the 1 key and the 9 key. After the first backspace, the content of the field is 2. After the second backspace, the content of the field is the empty string, but if allowsInvalid were false, this would not be allowed (since the empty string is not a valid integer), and the field would refuse to allow the 2 to be deleted. Setting this property to false can be effective in certain cases but should be done ...

Get Java Swing, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.