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
}

