我对JFormattedTextField加了一个过滤器,为什么在JFormattedTextField里面插入内容的时候,只执行了DocumentFilter接口的replace方法,怎么都不执行insertString方法呢?

解决方案 »

  1.   

    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);
    }
    }
      

  2.   

    这篇文章可能对你的问题有所帮助:
    http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5014885
      

  3.   

    不要用JFormattedTextField
    我在Blog里写了一个例子,你可以去看看。
      

  4.   

    http://blog.csdn.net/mq612/archive/2006/09/29/1305413.aspx
      

  5.   

    顺便请你帮我把这个贴子也给解了吧,very thanks
    http://community.csdn.net/Expert/topic/4964/4964120.xml?temp=.7958948
      

  6.   

    哦,还有,其实JFormattedTextField的功能还是蛮好用的,我只是不明白为什么insertString方法没有执行?(当然可以把动作写到replace方法里就可以代替)