I need a JTextField that only accepts positive integers as input. My implementation mostly works. As you type text into the field, the digits appear and non-digits do not appear.
But there is a problem with the constructor's "text" argument. If I pass a String that contains the String representation of a positive integer, I would expect that the text field would start out containing that field, but instead the text field starts out blank. For example, doing this causes it to exhibit the symptoms I've described: new NumericTextField("1", 5);
The over-ridden insertString method is not called during initialization.
What am I doing wrong, and how do I fix it? Here's the code...
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
/**
* A JTextField that only accepts characters that are digits. Also, the first character can't be "0".
*/
public class NumericTextField extends JTextField {
public class CustomFilterDocument extends PlainDocument {
public void insertString(int offset, String text, AttributeSet aset) throws BadLocationException {
if (isLegalValue(text)) {
super.insertString(offset, text, aset);
}
}
}
private static boolean isLegalValue(String text) {
if (text == null) {
return false;
}
int len = text.length();
for (int i = 0; i < len; i++) {
char c = text.charAt(i);
if (!Character.isDigit(c) || (i ==0 && c == '0')) {
return false;
}
}
return true;
}
public NumericTextField() {
super();
setDocument(new CustomFilterDocument());
}
public NumericTextField(int columns) {
super(columns);
setDocument(new CustomFilterDocument());
}
public NumericTextField(String text) {
super(text);
setDocument(new CustomFilterDocument());
}
public NumericTextField(String text, int columns) {
super(text, columns);
setDocument(new CustomFilterDocument());
}
}
The fixed version (using what I learned from the responses) looks like this:
public NumericTextField(String text) {
super(text);
initText(text);
}
public NumericTextField(String text, int columns) {
super(text, columns);
initText(text);
}
private void initText(String text) {
Document doc = new CustomFilterDocument();
try {
doc.insertString(0, text, null);
} catch (BadLocationException ble) {
}
setDocument(doc);
}