开发者

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

开发者 https://www.devze.com 2023-03-10 17:30 出处:网络
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

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

精彩评论

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