Article View: es.comp.lenguajes.java
Article #18038controlar 'Document'
From: Ft
Date: Wed, 04 Oct 2000 00:00
Date: Wed, 04 Oct 2000 00:00
57 lines
2493 bytes
2493 bytes
A continuaci�n mando un c�digo con el que controlo que el texto metido en
un JTextField tenga el formato dd-mm-aaaa, es decir, para fechas. Habr�a
que crear el JTextField con 'new JTextField( <objeto DateDocument>, null,
0 ).
El problema que tengo es que para controlarlo todo tomo la cadena del
PlainDocument y la repaso para luego volver a ponerla con
super.insertString() y cuando se 'inserta', el cursor se coloca al final
del texto... NO en la posici�n de la inserci�n...
Alguna idea para solucionar esto??
//-----------------------------------------------------------------------
import javax.swing.text.*;
public class DateDocument extends PlainDocument {
private final int MAX_COLS = 10;
private final char SEPARADOR = '-';
public void insertString( int offs, String str, AttributeSet a )
throws BadLocationException {
if( ( str == null ) | ( offs >= MAX_COLS ) )
return;
char[] tmp = str.toCharArray();
StringBuffer date = new StringBuffer();
date.ensureCapacity( tmp.length );
for( int i = 0; i < tmp.length; i++ )
if( Character.isDigit( tmp[i] ) )
date.append( tmp[i] );
super.insertString( offs, date.toString(), a );
// Esto puede elevar (imposible) una BadLocationException,
pero es throws
char[] tm2 = this.getText( 0, this.getLength()
).toCharArray();
StringBuffer dat2 = new StringBuffer();
dat2.ensureCapacity( MAX_COLS );
for( int i = 0; (i < tm2.length) & (i < MAX_COLS); i++ ) {
if ( ( i == 2 ) | ( i == 5 ) )
dat2.append( SEPARADOR );
if( tm2[i] != SEPARADOR )
dat2.append( tm2[i] );
}
super.remove( 0, this.getLength() );
super.insertString( 0, dat2.toString(), a );
// super.insertString( offs, "", a );
}
}
//-----------------------------------------------------------------------
Antes lo he mandado como attach pero parece que no ha ido bien... perd�n
si os encontrais esto 2 veces.
Technical Details
Message-ID:
<MPG.144585d62e2e7811989683@news.teleline.es>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: SPANISH,ASCII X-Google-Thread: 1116f3,cfd0f61cf5d1ddc0,start X-Google-Attributes: gid1116f3,public From: Ft <ft@NOSPANiwmail.com> Subject: controlar 'Document' Date: 2000/10/04 Message-ID: <MPG.144585d62e2e7811989683@news.teleline.es> X-Deja-AN: 677545756 X-Complaints-To: usenet@teleline.es X-Trace: telenews.teleline.es 970680343 62.82.98.105 (Wed, 04 Oct 2000 19:25:43 MET DST) Organization: The Exorcist NNTP-Posting-Date: Wed, 04 Oct 2000 19:25:43 MET DST Newsgroups: es.comp.lenguajes.java