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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
public class CustomTextField extends JTextField
{
public CustomTextField()
{
super();
}
public CustomTextField(String value)
{
super(value);
}
@Override
protected Document createDefaultModel()
{
return new FileCaseDocument();
}
static class FileCaseDocument extends PlainDocument
{
final int CHARS_TO_HIDE = 4;
int i;
char charArray[];
@Override
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException
{
if (str == null)
{
return;
}
if (str.length() == 1 && offs < CHARS_TO_HIDE)
{
str = "*";
}
// pour gérer le copier coller
if (str.length() > 1)
{
charArray = str.toCharArray();
i = 0;
while (i < charArray.length && i < CHARS_TO_HIDE)
{
charArray[i] = '*';
i++;
}
str = String.valueOf(charArray);
}
super.insertString(offs, str, a);
}
}
} |
Partager