views:

25

answers:

2

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);
}
+1  A: 

The problem is that you are setting the document to be a new document. This will erase any content that you pass in through the constructor.

public NumericTextField(String text) {
    super(text);
    // This next line will erase the content on the current document
    setDocument(new CustomFilterDocument());
}
jjnguy
A: 

A number of Swing components are not particularly clever when constructing with an existing model, which kind of works against the whole model idea.

Generally you shouldn't need to subclass JTextField (in fact, if you use DocumentFilter you don't need to subclass Document either). Insert the text into your document, create for text field and then set the document into the text field. There isn't really much overhead there.

If you really want to, you could subclass JTextField overriding the strangely named createDefaultModel. If you need some context variables, you can't pass them in through the constructor as that will not have finished executing by the time this method is called (indeed, don't touch this). However, if it is an anonymous inner class (and compiled with -target 1.4 or later), then you can access final local fields of the enclosing method.

Tom Hawtin - tackline