"Jason R. Kretzer" wrote:
I
have created a keypad using a JTextfield, 12 JButtons, and a JDialog window.
Is there a way to set a Listener on the JTextField that won't allow any
more than four characters to be typed in it?
Thanks,
-Jason
Jason,
Sun recommends that you handled this type of thing by modifying the
document model which the field uses. In the following example,
the "BoundedDocument" model both limits the data entry to a certain length
and optionally forces the entry to upper case.
Cynthia Jeness
------------------------------------------------------------------
package org.ajug.gui;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.Toolkit;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
/**
* This class adds additional functionality to the JTextField
so that the
* number of characters entered can be limited and optionally
converted
* to upper case.
*/
public class TextField extends JTextField {
/** Toolkit to use for reporting errors through "beep"
*/
private Toolkit toolkit;
/** Maximum length */
private int maxLength;
/** Flag indicating that upper case only */
boolean upperOnly;
/**
* This constructs the text entry field based on
the upper case
* option, the data entry width and the display width.
*
* @param upOnly True
if upper case only.
* @param elength Data entry
length.
* @param dlength Display
length.
*/
public TextField(boolean upOnly, int elength, int dlength)
{
super(dlength);
toolkit = Toolkit.getDefaultToolkit();
this.upperOnly = upOnly;
maxLength = elength;
setHorizontalAlignment(SwingConstants.LEFT);
}
/**
* This method overrides the method of the parent
class in order
* trim spaces from the string.
*
* @return Trimmed text contents.
*/
public String getText() {
String tmp = super.getText();
return tmp.trim();
}
/**
* This creates the default document model to use
the
* "BoundedDocument" model.
*
* @return WholeNumberDocumentModel object.
*/
protected Document createDefaultModel() {
return new BoundedDocument();
}
/**
* This class provides a document which will limit
the
* number of characters that a user can enter and
optionally
* force them to upper case.
*/
protected class BoundedDocument extends PlainDocument
{
/**
* This method limits the entry
to the maximum number of
* characters specified and optionally
forces upper case.
*
* @param offs
Offset into the entry.
* @param str
Entry string
* @param a
Attribute
*
* @exception
BadLocationException
*/
public void insertString(int offs,
String str,
AttributeSet a)
throws BadLocationException
{
// Check that the maximum
length is not exceeded.
if (maxLength > 0)
{
if ((getLength()+str.length() ) > maxLength) {
toolkit.beep();
return;
}
}
if (upperOnly) {
char[] source = str.toCharArray();
char[] result = new char[source.length];
for (int i=0; i < source.length; i++) {
result[i] = Character.toUpperCase(source[i]);
}
super.insertString(offs, new String(result, 0, result.length), a);
}
else
super.insertString(offs, str, a);
}
}
}
|