1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;
public class SecuredDocument extends PlainDocument {
public boolean isValidCharacter(char c) {
return c != 'a';
}
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (isValidCharacter(c))
super.insertString(offs, String.valueOf(c), a);
}
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPasswordField pasw = new JPasswordField(new SecuredDocument(), null,
30);
f.getContentPane().add(pasw);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
} |