Como fazer pra limitar a edicao do JTextField?

ola…

uma facil: como fazer pra limitar em x posicoes a edicao de em JTextField ?

obrigado !!!

eu usaria um JFormattedTextField

de uma olhada…

http://java.sun.com/docs/books/tutorial/uiswing/components/formattedtextfield.html

[quote=giba_f]ola…

uma facil: como fazer pra limitar em x posicoes a edicao de em JTextField ?

obrigado !!![/quote]

nesse link tem uma materia legal sobre isso
http://www.guj.com.br/article.show.logic?id=29

faz um tempo fiz esta classe:

public class TextField extends javax.swing.JTextField{

    private int maxLength = 0;
    
    private boolean upperCase = false;
    
    private boolean lowerCase = false;

    class TextDocument extends PlainDocument {

             public void insertString(int offs, String str, AttributeSet a) 
                 throws BadLocationException {
                    str = getString( str );
                    str = toUpperCase( str );
                    str = toLowerCase( str );
                    super.insertString(offs, str, a);
                 }
             
             public String getString( String str ){
                 if( getMaxLength() != 0 ){
                     int atualLength = super.getLength();
                     int length = getMaxLength() - atualLength;
                     return ( str.length() > length )? str.substring( 0, length ) : str;
                 }
                 return str;
             }
             
             public String toLowerCase( String str ){
                 return ( isLowerCase() )? str.toLowerCase() : str;
             }
             
             public String toUpperCase( String str ){
                 return ( isUpperCase() )? str.toUpperCase() : str;
             }
             
    }
 
    protected Document createDefaultModel() {
         return new TextDocument();
     }

    public int getMaxLength() {
        return maxLength;
    }

    public void setMaxLength(int maxLength) {
        this.maxLength = maxLength;
    }

    public boolean isUpperCase() {
        return upperCase;
    }

    public void setUpperCase(boolean upperCase) {
        this.upperCase = upperCase;
    }

    public boolean isLowerCase() {
        return lowerCase;
    }

    public void setLowerCase(boolean lowerCase) {
        this.lowerCase = lowerCase;
    }
    
}

talvez ajude!

nessa classe que foi postada e a mesma coisa do link que passei para inicializar ele

vc vai na classe onde estao os textfield e inicializa ele assim

public suaClasse() {
		super();
		initialize();
                seutextfield.setDocument(new TextDocument (30));// esse trita e a quantidade de caracteres que podera ser digitados.
	}

Apenas uma dica:

Construa um “core” para o seu sistema J2SE, customizando e herdando de todos esses componentes (JTextField, JComboBox) e entao utilize as SUAS classes.

Os componentes do core J2SE praticamente nao te dao suporte pra NADA.

blz…

obrigado a todos…

ja esta feito !

soh uma coisa: o que eh “CORE” ?

Engraçado, não me lembro de ter feito nenhum filho desses componentes… (embora efetivamente eu tenha feito uma classe de JComboBox “from scratch”).
Mas o JList, JTextField, JLabel, sempre funcionaram muito bem…