自己实现一个 DocumentFilter 示例代码如下import javax.swing.*;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class DocSizeFilter extends JFrame {
public static final int MAX_SIZE = 16;
private JTextField jtf; public DocSizeFilter() {
AbstractDocument aDoc; jtf = new JTextField();
aDoc = (AbstractDocument) jtf.getDocument();
aDoc.setDocumentFilter(new SizeFilter(MAX_SIZE));
getContentPane().add(jtf);
} public static void main(String[] args) {
DocSizeFilter frame = new DocSizeFilter();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
} private class SizeFilter extends DocumentFilter {
int maxSize;
int size;
Pattern p = Pattern.compile("[0-9]");
Matcher m = p.matcher(""); public SizeFilter(int maxSize) {
this.maxSize = maxSize;
} public void insertString(FilterBypass fb, int offset, String string,
AttributeSet attr) throws BadLocationException {
size = fb.getDocument().getLength() + string.length();
m.reset(string);
if (size < maxSize && m.matches()) {
super.insertString(fb, offset, string, attr);
}
} public void replace(DocumentFilter.FilterBypass fb, int offset,
int length,String text, AttributeSet attrs)
throws BadLocationException {
size = fb.getDocument().getLength() + text.length();
m.reset(text);
if (size < maxSize && m.matches()) {
super.insertString(fb, offset, text, attrs);
}
}
}
}
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class DocSizeFilter extends JFrame {
public static final int MAX_SIZE = 16;
private JTextField jtf; public DocSizeFilter() {
AbstractDocument aDoc; jtf = new JTextField();
aDoc = (AbstractDocument) jtf.getDocument();
aDoc.setDocumentFilter(new SizeFilter(MAX_SIZE));
getContentPane().add(jtf);
} public static void main(String[] args) {
DocSizeFilter frame = new DocSizeFilter();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
} private class SizeFilter extends DocumentFilter {
int maxSize;
int size;
Pattern p = Pattern.compile("[0-9]");
Matcher m = p.matcher(""); public SizeFilter(int maxSize) {
this.maxSize = maxSize;
} public void insertString(FilterBypass fb, int offset, String string,
AttributeSet attr) throws BadLocationException {
size = fb.getDocument().getLength() + string.length();
m.reset(string);
if (size < maxSize && m.matches()) {
super.insertString(fb, offset, string, attr);
}
} public void replace(DocumentFilter.FilterBypass fb, int offset,
int length,String text, AttributeSet attrs)
throws BadLocationException {
size = fb.getDocument().getLength() + text.length();
m.reset(text);
if (size < maxSize && m.matches()) {
super.insertString(fb, offset, text, attrs);
}
}
}
}
解决方案 »
- 为什么我的socket连接上了,但不能向服务端发送数据
- JAVA泛型...困惑了......
- JDK不能安装
- jdk目前最新的好像是1.6.这个环境如何配置?
- 一个JAVA问题散分
- 常量在内存中是如何表示的?例如:int i=3;这个3是个整数常量.怎么表示的呢?
- 如何让我选择水果列表框就显示水果列表,选择蔬菜列表框就显示蔬菜列表呢
- java能不能像c,c++那样开发硬件?
- 请教高手:如何把 Office Word 中的字(包括字体、字色、字类型等)复制到 java 中。急!在线...
- websphere的最新版本是多少?这分好拿哟!:)
- 一张图片,能知道某个坐标位置的信息吗?就是能否知道如 rect(10,10,40,10) 位置是什么字?
- 一个分割字符串的问题求教各位达人,如何将一个字符串分成一个字符串数组?(JDK是1.3的,所以不能使用1.4中的split方法)
*@author: WangXitao Date:2004/4/25
*@function: Create a TextField PlainDocument,which is allowed input Integer
*/import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*; public class WxtIntegerTextField extends JTextField{
private WxtIntegerDocument plainDoc=null;
public WxtIntegerTextField(){
super();
setDocument(plainDoc=new WxtIntegerDocument(this));
setHorizontalAlignment(JTextField.RIGHT);
}
}class WxtIntegerDocument extends PlainDocument{
private JTextComponent myComponent=null;
public WxtIntegerDocument(JTextComponent txtComponent){
myComponent=txtComponent;
}
public void insertString(int offset, String s,AttributeSet attributeSet) throws BadLocationException{
if (s != null) {
StringBuffer buffer = new StringBuffer(s);
for (int i = buffer.length() - 1; i >= 0; i--) {
char ch = buffer.charAt(i);
if (!Character.isDigit(ch) && ch != ',' )
buffer.deleteCharAt(i);
}
s = buffer.toString();
}
super.insertString(offset, s, attributeSet);
}
public void replace(int offset, int length, String s, AttributeSet attributeSet) throws BadLocationException {
String Value = myComponent.getText();
String str = "";
String Result = "";
if(myComponent.getText().length()>0){
str = myComponent.getText().substring(0,1);
if(s.equals("-")){
if(offset==0 && !(str.equals("-"))){
Result = s + Value;
}else{
Toolkit.getDefaultToolkit().beep(); //Transfer the System's ring
return;
}
}else{
Result = Value.substring(0,offset) + s + Value.substring(offset);
}
}else{
if(s.equals("-")){
//super.insertString(offset, s, attributeSet);
Toolkit.getDefaultToolkit().beep(); //Transfer the System's ring
return;
}else{
Result = s;
}
} try {
long value = Long.parseLong(Result);
if(value>999999 || value<=0) {
Toolkit.getDefaultToolkit().beep(); //Transfer the System's ring
return;
}
/**if(value>Integer.MAX_VALUE || value<Integer.MIN_VALUE) {
return;
}*/
}
catch(Exception ex) {
Toolkit.getDefaultToolkit().beep(); //Transfer the System's ring
return;
}
super.replace(offset, length, s, attributeSet);
}}
请参考/**
*@author: WangXitao Date:2004/4/25
*@function: Create a TextField,which is allowed input 0--9 and '-'
*/import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;public class WxtDateTextField extends JTextField{
private WxtDatePlainDocument plainDoc=null;
public WxtDateTextField(int maxLen){
super();
setDocument(plainDoc=new WxtDatePlainDocument(maxLen));
setHorizontalAlignment(JTextField.RIGHT);
}
public WxtDateTextField(){
super();
setDocument(plainDoc=new WxtDatePlainDocument());
setHorizontalAlignment(JTextField.RIGHT);
}
public void setMaxLength(int max){
plainDoc.setMaxLength(max);
}
public int getMaxLength(){
return plainDoc.getMaxLength();
}
}
class WxtDatePlainDocument extends PlainDocument{
private int maxLength=100; //The default max length of TextField
public WxtDatePlainDocument(int MaxLen){
this.maxLength=MaxLen;
}
public WxtDatePlainDocument(){}
public void setMaxLength(int max){
this.maxLength=max;
}
public int getMaxLength(){
return this.maxLength;
}
public void insertString(int offset,String s,AttributeSet a) throws BadLocationException{
int len=getLength();
String str=getText(0,len);
if ( (str+s).length()>maxLength ){
Toolkit.getDefaultToolkit().beep(); //Transfer the System's ring
return;
}
try{
java.util.StringTokenizer tokens = new java.util.StringTokenizer( str+s,"-" );
while( tokens.hasMoreElements() ) {
Integer.parseInt( (String)tokens.nextElement() ); //If parameter is not a number,
} //it will throw an Exception
}catch ( Exception e ){
Toolkit.getDefaultToolkit().beep();
return;
}
super.insertString(offset,s,a);
}
}