com.sas.swing.visuals
Class LimitedTextField

com.sas.swing.visuals.LimitedTextField
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, javax.accessibility.Accessible, javax.swing.Scrollable, javax.swing.SwingConstants

public class LimitedTextField

This class extends JTextField to use the LimitPlainDocument class as the default document. This private document class supports the component's maxCharacters property. If a user operation would exceed the maximum number of characters allowable, no new characters are entered. And, if the beepEnabled property (off by default) is enabled the control will beep when such operations are attempted. This class supports all the constructors of JTextField except the one which takes a document as one of the arguments - there is no utility to using this class if a document is supplied for use and in such a case the maxCharacters and beepEnabled properties are meaningless. Typical usage is as follows: LimitedTextField ltf = new LimitedTextField(); ltf.setMaxCharacters(32); // allow a max of 32 characters The implementation of the document class is based on the sample subclass shown in the 1.4 Javadoc for JTextField. Do note that in the current implementation of setText() is called with a string longer than maxCharacters any previous contents of the text field are erased. If this is an issue in a use case, a reimplementation of this class using DocumentFilters (introduced in 1.4) may be an option.

See Also:
Serialized Form

Constructor Summary
LimitedTextField()
          Constructs a new empty LimitedTextField - identical to the corresponding JTextField constructor.
LimitedTextField(int columns)
          Constructs a new empty LimitedTextField with the specified number of columns - identical to the corresponding JTextField constructor.
LimitedTextField(java.lang.String text)
          Constructs a new LimitedTextField initialized with the specified text - identical to the corresponding JTextField constructor.
LimitedTextField(java.lang.String text, int columns)
          Constructs a new LimitedTextField initialized with the specified text and columns.
 
Method Summary
protected  javax.swing.text.Document createDefaultModel()
           
 int getMaxCharacters()
          Gets the maximum number of characters the LimitedTextField will allow.
 boolean isBeepEnabled()
          Gets the value of the beepEnabled property.
 void setBeepEnabled(boolean beepEnabled)
          Sets the beepEnabled property.
 void setMaxCharacters(int maxCharacters)
          Sets the maximum number of characters the LimitedTextField will allow.
 

Constructor Detail

LimitedTextField

public LimitedTextField()
Constructs a new empty LimitedTextField - identical to the corresponding JTextField constructor.


LimitedTextField

public LimitedTextField(int columns)
Constructs a new empty LimitedTextField with the specified number of columns - identical to the corresponding JTextField constructor.

Parameters:
columns - the number of columns to use to calculate the preferred width

LimitedTextField

public LimitedTextField(java.lang.String text)
Constructs a new LimitedTextField initialized with the specified text - identical to the corresponding JTextField constructor.

Parameters:
text - the initial text

LimitedTextField

public LimitedTextField(java.lang.String text,
                        int columns)
Constructs a new LimitedTextField initialized with the specified text and columns.

Parameters:
text - the initial text
columns - the number of columns to use to calculate the preferred width
Method Detail

createDefaultModel

protected javax.swing.text.Document createDefaultModel()
Overrides:
createDefaultModel in class javax.swing.JTextField

getMaxCharacters

public int getMaxCharacters()
Gets the maximum number of characters the LimitedTextField will allow.

Returns:
the maximum number of characters allowed in the LimitedTextField

setMaxCharacters

public void setMaxCharacters(int maxCharacters)
Sets the maximum number of characters the LimitedTextField will allow.

Parameters:
maxCharacters - the maximum number of characters allowed in the LimitedTextField

isBeepEnabled

public boolean isBeepEnabled()
Gets the value of the beepEnabled property.

Returns:
the value of the beepEnabled property.

setBeepEnabled

public void setBeepEnabled(boolean beepEnabled)
Sets the beepEnabled property.

Parameters:
beepEnabled - the value to set



Copyright © 2009 SAS Institute Inc. All Rights Reserved.