public JTextField(Document doc, String text, int columns)Constructs a new JTextField that uses the given text storage model and the given number of columns. This is the constructor through which the other constructors feed. If the document is null, a default model is created.
Parameters:
doc - the text storage to use. If this is null, a default will be provided by calling the createDefaultModel method.
text - the initial string to display, or null
columns - the number of columns to use to calculate the preferred width >= 0. If columns is set to zero, the preferred width will be whatever naturally results from the component implementation.
Throws:
IllegalArgumentException - if columns < 0
Parameters:
doc - the text storage to use. If this is null, a default will be provided by calling the createDefaultModel method.
text - the initial string to display, or null
columns - the number of columns to use to calculate the preferred width >= 0. If columns is set to zero, the preferred width will be whatever naturally results from the component implementation.
Throws:
IllegalArgumentException - if columns < 0
class mydoc extends Document{
//重载insert方法,每输入一个字符判断是否为数字,Integer.parseInt()..
}
import javax.swing.*;
import javax.swing.text.*;
import java.util.*;
import java.text.*;public class formatted {
public static void main (String args[]) throws ParseException {
JFrame f = new JFrame("JFormattedTextField Sample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = f.getContentPane();
content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
// Four-digit year, followed by month name and day of month,
// each separated by two dashes (--)
DateFormat format = new SimpleDateFormat("yyyy--MMMM--dd");
DateFormatter df = new DateFormatter(format);
JFormattedTextField ftf1 = new JFormattedTextField(df);
ftf1.setValue(new Date());
content.add(ftf1);
// US Social Security number
MaskFormatter mf1 = new MaskFormatter("###-##-####");
mf1.setPlaceholderCharacter('_');
JFormattedTextField ftf2 = new JFormattedTextField(mf1);
content.add(ftf2);
// US telephone number
MaskFormatter mf2 = new MaskFormatter("(###) ###-####");
JFormattedTextField ftf3 = new JFormattedTextField(mf2);
content.add(ftf3);
f.setSize(300, 100);
f.show();
}
}
import javax.swing.text.*; import java.awt.Toolkit;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;public class WholeNumberField extends JTextField {
private Toolkit toolkit;
private NumberFormat integerFormatter; public WholeNumberField(int value, int columns) {
super(columns);
toolkit = Toolkit.getDefaultToolkit();
integerFormatter = NumberFormat.getNumberInstance(Locale.US);
integerFormatter.setParseIntegerOnly(true);
setValue(value);
} public int getValue() {
int retVal = 0;
try {
retVal = integerFormatter.parse(getText()).intValue();
} catch (ParseException e) {
// This should never happen because insertString allows
// only properly formatted data to get in the field.
toolkit.beep();
}
return retVal;
} public void setValue(int value) {
setText(integerFormatter.format(value));
} protected Document createDefaultModel() {
return new WholeNumberDocument();
} protected class WholeNumberDocument extends PlainDocument {
public void insertString(int offs,
String str,
AttributeSet a)
throws BadLocationException {
char[] source = str.toCharArray();
char[] result = new char[source.length];
int j = 0; for (int i = 0; i < result.length; i++) {
if (Character.isDigit(source[i]))
result[j++] = source[i];
else {
toolkit.beep();
System.err.println("insertString: " + source[i]);
}
}
super.insertString(offs, new String(result, 0, j), a);
}
}
}