It is easy
你继承javax.swing.text.PlainDocument再做一个Documnet实现
重写insertString(..)方法,其中进行你的判断,最后不要忘记写super.insertString(..)
你继承javax.swing.text.PlainDocument再做一个Documnet实现
重写insertString(..)方法,其中进行你的判断,最后不要忘记写super.insertString(..)
TextField bm = new TextField();
bm.setDocument(new CustomTextFormator(max_length));
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
public class CustomTextFormator extends PlainDocument
{ private String strAllowChar =null;
private int intMaxLength =0;
private int intDataType=0;
private int intPricesion=0;
public static final int INTEGER_TEXT=1;
public static final int FLOAT_TEXT=2;
public static final int DATE_YEAR=3;
public static final int DATE_MONTH=4;
//--- Construct Function
public CustomTextFormator(int intMaxLength)
{ super();
this.intMaxLength = intMaxLength;
}
//----
public CustomTextFormator(int intMaxLength, String strCharList)
{ super();
this.intMaxLength = intMaxLength;
this.strAllowChar = strCharList;
}
//----
public CustomTextFormator(int intMaxLength, int intDataType)
{ super();
this.intMaxLength = intMaxLength;
this.intDataType = intDataType;
if(intDataType == this.INTEGER_TEXT)
this.strAllowChar = "-1234567890";
else if(intDataType == this.DATE_YEAR)
this.strAllowChar = "1234567890";
else if(intDataType == this.DATE_MONTH)
this.strAllowChar = "1234567890";
else if(intDataType == this.FLOAT_TEXT)
this.strAllowChar = "-1234567890.";
}
//----
public CustomTextFormator(int intMaxLength, int intDataType, int intPricesion)
{ super();
this.intMaxLength = intMaxLength;
this.intDataType = intDataType;
if(intDataType == this.INTEGER_TEXT)
this.strAllowChar = "-1234567890";
else if(intDataType == this.FLOAT_TEXT)
this.strAllowChar = "-1234567890.";
this.intPricesion = intPricesion;
}
//==== Check Function Panel ===
public void insertString(int offset, String s, AttributeSet attributeSet) throws BadLocationException
{
if (!(s == null)) {
//-- Check Length
String strLastText = super.getText(0,super.getLength());
if(this.intMaxLength>0)
{ /*if(s.length()>1)
{ if(s.getBytes().length+strLastText.getBytes().length>=(this.intMaxLength+this.intPricesion+1))
s=s.substring(0,this.intMaxLength-strLastText.getBytes().length);
}
else*/
{ int intIntegerPos = strLastText.indexOf(".");
if(intIntegerPos >=0)
{ if(intIntegerPos>this.intMaxLength)
return;
if(strLastText.getBytes().length>=(this.intMaxLength+this.intPricesion+1))
return;
}
else
{ if(strLastText.getBytes().length>=this.intMaxLength && (!s.equals(".")))
return;
}
}
}
//-- Check Char String--
if(this.strAllowChar instanceof String)
{ String strCheckChar=null;
for(int i=0;i<s.length();i++)
{ strCheckChar=s.substring(i,i+1);
if(this.strAllowChar.indexOf(strCheckChar)<0)
return;
}
}
//---- Check Signed Position
int intSignedPos = s.trim().indexOf("-");
if(intSignedPos >0 && offset>0)
return;
//----
if(this.intDataType == this.DATE_MONTH)
{ if(strLastText.length()>0)
{ if(strLastText.compareTo("1")>0)
return;
else if(strLastText.compareTo("1")==0 && s.compareTo("2")>0)
return;
else if(strLastText.compareTo("0")==0 && s.compareTo("0")==0)
return;
}
}
//--- Check Precision -
else if(this.intPricesion >0)
{ int intDotPos=strLastText.lastIndexOf(".");
if(s.indexOf(".")>=0)
{ if(intDotPos >0)
return; //--- 已有点号
else //--- 插入点号
{ int intCurrentDotPos = s.indexOf(".");
if((s.length()-intCurrentDotPos-1) + (strLastText.length()-offset) > this.intPricesion)
return;
} }
if(intDotPos>=0 && (strLastText.trim().length()-intDotPos>this.intPricesion)
&& offset>intDotPos)
return;
}
}
super.insertString(offset,s,attributeSet);
}
//============================
}