开发者

How to have a default value on a jTextField that is being validated?

I want to validate the input of a jTextField and also have a default value set. From what I understand, this code should work, but the default value never appears. If I remove the validation code, the default value appears as it should. Is there a way that I can have my input validation AND have a default value also?

   myInput.setDocument(new javax.swing.text.PlainDocument(){
        @Override
        public void insertString(int offs, String str, javax.swing.text.AttributeSet a)
        throws javax.swing.text.BadLocationException {
            if((getLength() + str.length()) <= 开发者_开发知识库15
                && str.matches("[0-9\\.]"))
            super.insertString(offs, str, a);
        }
    });
    myInput.setText("127.0.0.1");


Maybe reason is that setText("127.0.0.1") makes insertString be called with a str parameter of "127.0.0.1", which does not match that regexp you are using. So I think you might replace it with "[0-9\.]+" which will match that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜