Bonjour,

Après avoir créé une classe qui hérite de PlainDocument, je me rend compte qu'au moment de l'appel de insertString(), le caret se positionne automatiquement à la fin (de mon JTextField dans mon cas).

Voici le code de la classe interne qui hérite de PlainDocument :
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
	private class MonPlainDocument extends PlainDocument
	{
			@Override
			public void insertString(int off, String s, AttributeSet a) throws BadLocationException
			{
				int nbCurrentChar = this.getLength();
				// Si on a atteint la limite, on quitte...
				if ((s == null)||((nbCurrentChar == maxlength) && (maxlength > 0)) ) 
					return;						
				// On détecte la chaîne à ajouter
				StringBuffer str = new StringBuffer();
				if(authorizedCharacters == null)
					for (int i = 0; i < s.length(); i++) 
						str.append(new String(s.charAt(i) + ""));
				else
					for (int i = 0; i < s.length(); i++) 
						if(authorizedCharacters.contains(s.charAt(i)))
							str.append(new String(s.charAt(i) + ""));
 
				if (str.toString().equals("")) 
					return;   
				else if ((maxlength == 0)||((nbCurrentChar + str.length()) <= maxlength)) 
					super.insertString(off, str.toString() + "", a); 
				else 
					// on doit couper la chaîne à insérer car on dépasserait le
					// nombre de caractères autorisés.
					super.insertString(off, str.substring(0, (maxlength - nbCurrentChar) ), a);
			}
	}
Quelqu'un aurait-il une solution afin que le caret ne se positionne pas à la fin après l'insertString() ?

Merci.