A voir également:
- LONGUEUR LIMITE D'UN TEXT DANS UN TextField
- Sms to text - Télécharger - Sauvegarde
- Mettre un texte en majuscule - Guide
- Appel limité - Forum Mobile
- Comment enlever la limite d'ajout sur snapchat - Forum Snapchat
- Comment justifier un texte sur word ✓ - Forum Word
4 réponses
Pour info cela ne marche pas si on fait un copier-coller d'un texte trop long dans le textfield.
Il vaut mieux faire ceci :
import java.awt.Toolkit;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
/**
* TextField that can be limited in size (max number of characters typed in)
* @author oma
*
*/
public class LimitedTextField extends JTextField {
public LimitedTextField(int maxLength)
{
super();
AbstractDocument doc = (AbstractDocument) getDocument();
doc.setDocumentFilter(new TextLimiter(maxLength));
}
/**
* Text limiter used to limit the number of characters of text fields
* Should be used this way :
*
* AbstractDocument doc = (AbstractDocument) myTextComponent.getDocument();
doc.setDocumentFilter(new TextLimiter(maxLength));
*
* @author oma
*
*/
private class TextLimiter extends DocumentFilter {
private int max;
public TextLimiter(int max) {
this.max = max;
}
public void insertString(DocumentFilter.FilterBypass fb, int offset, String str, AttributeSet attr) throws BadLocationException {
replace(fb, offset, 0, str, attr);
}
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String str, AttributeSet attrs) throws BadLocationException {
int newLength = fb.getDocument().getLength() - length + str.length();
if (newLength <= max) {
fb.replace(offset, length, str, attrs);
} else {
Toolkit.getDefaultToolkit().beep();
}
}
}
}
Il vaut mieux faire ceci :
import java.awt.Toolkit;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
/**
* TextField that can be limited in size (max number of characters typed in)
* @author oma
*
*/
public class LimitedTextField extends JTextField {
public LimitedTextField(int maxLength)
{
super();
AbstractDocument doc = (AbstractDocument) getDocument();
doc.setDocumentFilter(new TextLimiter(maxLength));
}
/**
* Text limiter used to limit the number of characters of text fields
* Should be used this way :
*
* AbstractDocument doc = (AbstractDocument) myTextComponent.getDocument();
doc.setDocumentFilter(new TextLimiter(maxLength));
*
* @author oma
*
*/
private class TextLimiter extends DocumentFilter {
private int max;
public TextLimiter(int max) {
this.max = max;
}
public void insertString(DocumentFilter.FilterBypass fb, int offset, String str, AttributeSet attr) throws BadLocationException {
replace(fb, offset, 0, str, attr);
}
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String str, AttributeSet attrs) throws BadLocationException {
int newLength = fb.getDocument().getLength() - length + str.length();
if (newLength <= max) {
fb.replace(offset, length, str, attrs);
} else {
Toolkit.getDefaultToolkit().beep();
}
}
}
}
Pumbaa
C'est absolument parfait djmalo !!!
Ou, si tu tu veux rendre la chose encore plus agréable tu peux limiter toi même avec un listener , tu te fais un petite classe du style :
public class MonTextField extends JTextField {
private boolean utilityKeyPressed=false;
private int maxChar = 10;
public MonTextField(int max) {
super();
maxChar = max;
this.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) { checkutilityKey(e); }
public void keyTyped(KeyEvent e){ checklenght(e); }
public void keyReleased(KeyEvent e){ e.consume(); }
}
);
}
/*Vérification des touches BACKSPACE,LEFT,RIGHT pour les gardées actives*/
private void checkutilityKey(KeyEvent e) {
utilityKeyPressed = (e.getKeyCode()==e.VK_BACK_SPACE || e.getKeyCode()==e.VK_LEFT || e.getKeyCode()==e.VK_RIGHT || e.getKeyCode()==e.VK_UP || e.getKeyCode()==e.VK_DOWN);
}
/*Vérification de la longueur*/
private void checklenght(KeyEvent e) {
if(!utilityKeyPressed && this.getText().length()=>maxChar)e.consume();
}
}
Et voilà, tu as une petite class qui t'affiche un JTextfield avec un max de caractère.
public class MonTextField extends JTextField {
private boolean utilityKeyPressed=false;
private int maxChar = 10;
public MonTextField(int max) {
super();
maxChar = max;
this.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) { checkutilityKey(e); }
public void keyTyped(KeyEvent e){ checklenght(e); }
public void keyReleased(KeyEvent e){ e.consume(); }
}
);
}
/*Vérification des touches BACKSPACE,LEFT,RIGHT pour les gardées actives*/
private void checkutilityKey(KeyEvent e) {
utilityKeyPressed = (e.getKeyCode()==e.VK_BACK_SPACE || e.getKeyCode()==e.VK_LEFT || e.getKeyCode()==e.VK_RIGHT || e.getKeyCode()==e.VK_UP || e.getKeyCode()==e.VK_DOWN);
}
/*Vérification de la longueur*/
private void checklenght(KeyEvent e) {
if(!utilityKeyPressed && this.getText().length()=>maxChar)e.consume();
}
}
Et voilà, tu as une petite class qui t'affiche un JTextfield avec un max de caractère.
Encore plus simple !
jTextField.addKeyListener( new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent e) {
if ( jTextField.getText().length()+1 >= max_length )
e.consume();
}
});
jTextField.addKeyListener( new java.awt.event.KeyAdapter() {
public void keyTyped(java.awt.event.KeyEvent e) {
if ( jTextField.getText().length()+1 >= max_length )
e.consume();
}
});