1 19 20 package org.netbeans.modules.db.util; 21 22 import javax.swing.JTextField ; 23 import java.awt.Toolkit ; 24 25 public class ValidableTextField extends JTextField 26 { 27 private TextFieldValidator validator = null; 28 29 static final long serialVersionUID =3686208002682293243L; 30 public ValidableTextField(TextFieldValidator val) 31 { 32 super(); 33 setValidator(val); 34 } 35 36 public TextFieldValidator getValidator() 37 { 38 return validator; 39 } 40 41 public void setValidator(TextFieldValidator val) 42 { 43 validator = val; 44 } 45 46 protected void reflectInvalidValue(String oldval, String newval) 47 { 48 setText(oldval); 49 Toolkit.getDefaultToolkit().beep(); 50 } 51 52 public void replaceSelection(String s) 53 { 54 String oldText = getText(); 55 super.replaceSelection(s); 56 if (validator != null && !validator.accepts(getText())) { 57 reflectInvalidValue(oldText, s); 58 } 59 } 60 } 61 70 | Popular Tags |