JFormattedTextField实现DocumentFilter接口问题 我对JFormattedTextField加了一个过滤器,为什么在JFormattedTextField里面插入内容的时候,只执行了DocumentFilter接口的replace方法,怎么都不执行insertString方法呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.awt.*;import java.awt.event.*;import java.lang.reflect.*;import java.net.*;import java.text.*;import java.util.*;import javax.swing.*;import javax.swing.text.*;/** A program to test formatted text fields*/public class FormatTest{ public static void main(String[] args) { FormatTestFrame frame=new FormatTestFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); }}/** A frame with a collection of formatted text fields and a button that displays the field values.*/class FormatTestFrame extends JFrame{ public FormatTestFrame() { setTitle("FormatTest"); setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT); Container contentPane=getContentPane(); JPanel buttonPanel=new JPanel(); okButton=new JButton("Ok"); buttonPanel.add(okButton); contentPane.add(buttonPanel,BorderLayout.SOUTH); mainPanel=new JPanel(); mainPanel.setLayout(new GridLayout(0,3)); contentPane.add(mainPanel,BorderLayout.CENTER); JFormattedTextField intField3=new JFormattedTextField(new InternationalFormatter(NumberFormat.getIntegerInstance()) { protected DocumentFilter getDocumentFilter() { return filter; } private DocumentFilter filter=new IntFilter(); }); intField3.setValue(new Integer(100)); addRow("Filtered Number:",intField3); } /** Adds a row to the main panel. @param labelText the lavel of the field @param field the sample field */ public void addRow(String labelText,final JFormattedTextField field) { mainPanel.add(new JLabel(labelText)); final JLabel valueLabel=new JLabel(); okButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { try { field.commitEdit(); } catch(ParseException exception) { exception.printStackTrace(); } Object value=field.getValue(); if(value.getClass().isArray()) { StringBuffer buffer=new StringBuffer(); buffer.append('{'); for(int i=0;i<Array.getLength(value);i++) { if(i>0)buffer.append(','); buffer.append(Array.get(value,i).toString()); } buffer.append('}'); valueLabel.setText(buffer.toString()); } else valueLabel.setText(value.toString()); } }); mainPanel.add(field); mainPanel.add(valueLabel); } public static final int DEFAULT_WIDTH=500; public static final int DEFAULT_HEIGHT=500; private JButton okButton; private JPanel mainPanel;}/** A filter that restricts input to digits and a '-' sign.*/class IntFilter extends DocumentFilter{ public void insertString(DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException { //问题就是这句不会运行,也就是这个方法没有执行 System.out.println("insertString"); StringBuffer buffer=new StringBuffer(); for(int i=buffer.length()-1;i>=0;i--) { char ch=buffer.charAt(i); if(!Character.isDigit(ch) && ch!='-') buffer.deleteCharAt(i); } super.insertString(fb,offset,buffer.toString(),attr); } public void replace(FilterBypass fb,int offset,int length,String string,AttributeSet attr) throws BadLocationException { System.out.println("replace"); StringBuffer buffer=new StringBuffer(); System.out.println("replace buffer:"+buffer); //上面的buffer值应该如何读取到剪贴板的值呢? if(string!=null) { int i=0; while(i<string.length()) { char ch=string.charAt(i); if(Character.isDigit(ch)||ch=='-')i++; else break; } string=string.substring(0,i); System.out.println("replace string:"+string); } else { for(int i=buffer.length()-1;i>=0;i--) { char ch=buffer.charAt(i); if(!Character.isDigit(ch) && ch!='-') buffer.deleteCharAt(i); } string=buffer.toString(); } super.replace(fb,offset,length,string,attr); } public void remove(DocumentFilter.FilterBypass fb,int offset,int length) throws BadLocationException { System.out.println("remove"); super.remove(fb,offset,length); }} 这篇文章可能对你的问题有所帮助:http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5014885 不要用JFormattedTextField我在Blog里写了一个例子,你可以去看看。 http://blog.csdn.net/mq612/archive/2006/09/29/1305413.aspx 顺便请你帮我把这个贴子也给解了吧,very thankshttp://community.csdn.net/Expert/topic/4964/4964120.xml?temp=.7958948 哦,还有,其实JFormattedTextField的功能还是蛮好用的,我只是不明白为什么insertString方法没有执行?(当然可以把动作写到replace方法里就可以代替) 可以让i!=i吗? Console是干什么用的?为什么我的小代码出错了。。 如何根据控制台的输入年月获取这个月的天数 请问各位高手,怎么用图形从数据库实时反应工程的进度,用什么类?~~急急 把JAVA源代码生成结构的软件? 求获取内存总容量与空闲内存的程序代码或者程序 紧急求助---java本地化问题 高分请教:如何在Jbuilder6中运行jar?急!!! EntityBean里面可以不设置主键吗? 谁能告诉我borland application server4.5.1 的序列码??? 请教FileDialog为什么设置FilenameFilter无效 哪位高手会写组织机构的校验代码?
import java.awt.event.*;
import java.lang.reflect.*;
import java.net.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
import javax.swing.text.*;/**
A program to test formatted text fields
*/
public class FormatTest
{
public static void main(String[] args)
{
FormatTestFrame frame=new FormatTestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}/**
A frame with a collection of formatted text fields and a button that displays the field values.
*/
class FormatTestFrame extends JFrame
{
public FormatTestFrame()
{
setTitle("FormatTest");
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT); Container contentPane=getContentPane(); JPanel buttonPanel=new JPanel();
okButton=new JButton("Ok");
buttonPanel.add(okButton);
contentPane.add(buttonPanel,BorderLayout.SOUTH); mainPanel=new JPanel();
mainPanel.setLayout(new GridLayout(0,3));
contentPane.add(mainPanel,BorderLayout.CENTER); JFormattedTextField intField3=new JFormattedTextField(new InternationalFormatter(NumberFormat.getIntegerInstance())
{
protected DocumentFilter getDocumentFilter()
{
return filter;
}
private DocumentFilter filter=new IntFilter();
});
intField3.setValue(new Integer(100));
addRow("Filtered Number:",intField3);
} /**
Adds a row to the main panel.
@param labelText the lavel of the field
@param field the sample field
*/
public void addRow(String labelText,final JFormattedTextField field)
{
mainPanel.add(new JLabel(labelText));
final JLabel valueLabel=new JLabel();
okButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
try
{
field.commitEdit();
}
catch(ParseException exception)
{
exception.printStackTrace();
}
Object value=field.getValue();
if(value.getClass().isArray())
{
StringBuffer buffer=new StringBuffer();
buffer.append('{');
for(int i=0;i<Array.getLength(value);i++)
{
if(i>0)buffer.append(',');
buffer.append(Array.get(value,i).toString());
}
buffer.append('}');
valueLabel.setText(buffer.toString());
}
else
valueLabel.setText(value.toString());
}
}); mainPanel.add(field);
mainPanel.add(valueLabel);
} public static final int DEFAULT_WIDTH=500;
public static final int DEFAULT_HEIGHT=500; private JButton okButton;
private JPanel mainPanel;
}/**
A filter that restricts input to digits and a '-' sign.
*/
class IntFilter extends DocumentFilter
{
public void insertString(DocumentFilter.FilterBypass fb,
int offset,
String string,
AttributeSet attr)
throws BadLocationException
{
//问题就是这句不会运行,也就是这个方法没有执行
System.out.println("insertString"); StringBuffer buffer=new StringBuffer();
for(int i=buffer.length()-1;i>=0;i--)
{
char ch=buffer.charAt(i);
if(!Character.isDigit(ch) && ch!='-')
buffer.deleteCharAt(i);
} super.insertString(fb,offset,buffer.toString(),attr);
} public void replace(FilterBypass fb,int offset,int length,String string,AttributeSet attr)
throws BadLocationException
{
System.out.println("replace");
StringBuffer buffer=new StringBuffer();
System.out.println("replace buffer:"+buffer);
//上面的buffer值应该如何读取到剪贴板的值呢? if(string!=null)
{
int i=0;
while(i<string.length())
{
char ch=string.charAt(i);
if(Character.isDigit(ch)||ch=='-')i++;
else break;
}
string=string.substring(0,i);
System.out.println("replace string:"+string);
}
else
{
for(int i=buffer.length()-1;i>=0;i--)
{
char ch=buffer.charAt(i);
if(!Character.isDigit(ch) && ch!='-')
buffer.deleteCharAt(i);
}
string=buffer.toString();
} super.replace(fb,offset,length,string,attr);
} public void remove(DocumentFilter.FilterBypass fb,int offset,int length)
throws BadLocationException
{
System.out.println("remove");
super.remove(fb,offset,length);
}
}
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5014885
我在Blog里写了一个例子,你可以去看看。
http://community.csdn.net/Expert/topic/4964/4964120.xml?temp=.7958948