Bonjour.

Jai un JTextField limité en nombres de caractères grace à ma class LimitDocumentPlain :



Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
public class LimitDocumentPlain extends PlainDocument 
{
 
private int limit;
 
public LimitDocumentPlain(int limit) 
{
  super();
  setLimit(limit);  // store the limit    
}
 
 
public void insertString(int offset, String s, AttributeSet attributeSet) 
{
  try {
 
    if(this.getLength()>=limit)
      return ;
 
    if(offset < limit) 
    // if we haven't reached the limit, insert the string
    {
        super.insertString(offset,s,attributeSet);
    } // otherwise, just lose the string
 
  }
  catch(BadLocationException e) 
  {}
}
 
 
}
Elle marche très bien, l'utilisateur est bien bloqué quand il entre "manuellement" les caractères. Par contre, en passant par les fonctions standard Ctrl-C puis Ctrl-V, il peut entrer plus de caractères !!
Comment faire ?