Why it doesn't work?

I create a DateMask input text but it stop at the fifth character and I couldn't find out why.Can someone show me how to fix this problem?

import javax.swing.*;

import javax.swing.text.*;

import java.awt.*;

import java.awt.event.*;

public class DateMask extends JTextField {

private JTextField tf = new JTextField(DateDoc.initSet);

public DateMask()

{

super.setDocument(new DateDoc(tf));

}

}

class DateDoc extends PlainDocument{

public static String initSet ="0000/00/00";

private static int sep1 = 4 , sep2 = 7; //Location of separators

private JTextComponent textCompo;

private int newOffset;

protected DateDoc(JTextComponent tc){

textCompo = tc;

try{

insertString(0,initSet,null);

}catch(Exception ex){}

}

public void insertString(int offset, String s, AttributeSet as)throws BadLocationException{

if(s.equals(initSet))

{

super.insertString(offset, s, as);

}

else

{

try

{

Integer.parseInt(s);

}

catch(Exception ex)

{

return ;

}

newOffset = offset;

if(atSeparator(offset)){

newOffset++;

textCompo.setCaretPosition(newOffset);

}

super.remove(newOffset, 1);

super.insertString(newOffset,s,as);

}

}//endinsert

public void remove(int offset, int length) throws BadLocationException{

if(atSeparator(offset)){

textCompo.setCaretPosition(offset -1 );

}

else{

textCompo.setCaretPosition(offset);

}

}//endremove

public boolean atSeparator(int offset){

return offset == sep1 || offset == sep2;

}//endseparators

}

[1781 byte] By [TQnguyen] at [2007-9-26 2:54:44]
# 1

note the changes to your DateMask class. this works as you expect

public class DateMask extends JTextField {

//private JTextField tf = new JTextField(DateDoc.initSet);

public DateMask() {

super(DateDoc.initSet);

super.setDocument(new DateDoc(this));//tf));

}

}

i can't explain why this works.

though i came up with this solution because i noticed that the caret position as obtained with getCaretPosition was always 0, before you were setting it.

somehow the setCaretPosition wasn't setting the caret position. i think these two are related.

the thing is that there was an instance of a JTextField within your custom JTextField component.

so i guess it was some sort of a conflict between the two of them.

but i might be completly off here.

hopefully somebody can explain this behaviour

partha

parthasarkar at 2007-6-29 10:44:27 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 2
Thank for your help.This bug made me crazy for weeks.
TQnguyen at 2007-6-29 10:44:27 > top of Java-index,Archived Forums,New To Java Technology Archive...