【问题标题】:Symbols validation in JTextFieldJTextField 中的符号验证
【发布时间】:2012-11-16 07:56:21
【问题描述】:

我想要做的是当我们要在 TextField 中输入内容时,它不应该允许符号。如何排除符号?

我使用的代码:

 if (evt.getKeyChar()=='&'||evt.getKeyChar()=='@') {
       jTextField2.setText("");
    }

【问题讨论】:

标签: java swing jtextfield keylistener documentfilter


【解决方案1】:

您应该使用DocumentFilter。这是一个非常简单/基本的演示示例:

import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;

public class TestDocumentFilter {

    private void initUI() {
        JFrame frame = new JFrame(TestDocumentFilter.class.getSimpleName());
        frame.setLayout(new FlowLayout());
        final JTextField textfield = new JTextField(20);
        ((AbstractDocument) textfield.getDocument()).setDocumentFilter(new DocumentFilter() {
            @Override
            public void insertString(FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
                string = string.replace("&", "").replace("@", "");
                super.insertString(fb, offset, string, attr);
            }

            @Override
            public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
                text = text.replace("&", "").replace("@", "");
                super.replace(fb, offset, length, text, attrs);
            }
        });
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(textfield);
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestDocumentFilter().initUI();
            }
        });
    }

}

【讨论】:

    【解决方案2】:

    JFormattedTextField

    它有两个属性,而不是 JTextField 中的一个:

    (1)字符串“文本”显示和(2)任何类“值”

    你只需要放置一个格式化器来转换 stringToValue() 和 valueToString()

    import java.text.ParseException;
    import javax.swing.JFormattedTextField;
    import javax.swing.text.DefaultFormatter;
    import javax.swing.text.JTextComponent;
    
    ...
    
      DefaultFormatter formatter = new DefaultFormatter() {
        {
          setValueClass(String.class); // property "value" of String.class
          setAllowsInvalid(false); // doesnt matter in current example, but very usefull
          setCommitsOnValidEdit(true); // convert value back to text after every valid edit
        }
        @Override
        public Object stringToValue(String string) throws ParseException {
          string = string.replace("&","").replace("@","");
          return string;
        }
      };
    
      JTextComponent txt = new JFormattedTextField(formatter);
    

    你还可以

      txt.addPropertyChangeListener("value", yourPropertyChangeListener);
    

    改变后立即获得价值

    【讨论】:

      猜你喜欢
      • 2012-12-31
      • 1970-01-01
      • 2012-12-11
      • 2020-11-19
      • 1970-01-01
      • 2017-07-20
      • 1970-01-01
      • 2012-12-19
      • 2013-10-01
      相关资源
      最近更新 更多