键入时更改jTextField的内容

时间:2013-09-12 14:34:46

标签: java swing while-loop jtextfield clear

我正在尝试重置我的JTextField,以防输入Space。它抛出了我:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Attempt to mutate in notification

我的代码是:

import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class MainFrame extends JFrame {

    private JPanel contentPane;
    private JTextField textField;

    /** * Launch the application. */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainFrame frame = new MainFrame();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /** * Create the frame. */
    public MainFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JDesktopPane desktopPane = new JDesktopPane();
        desktopPane.setBackground(Color.LIGHT_GRAY);
        desktopPane.setBounds(6, 6, 438, 266);
        contentPane.add(desktopPane);

        textField = new JTextField();
        textField.getDocument().addDocumentListener(new DocumentListener() {
            public void changedUpdate(DocumentEvent e) {
            }

            public void removeUpdate(DocumentEvent e) {
            }

            public void insertUpdate(DocumentEvent e) {
                if (textField.getText().contains(" ")) {
                    System.out
                            .println("Space was entered! clearing the  text field");
                    textField.setText("");
                }
            }
        });
        textField.setBounds(133, 112, 134, 28);
        desktopPane.add(textField);
        textField.setColumns(10);
    }
}

2 个答案:

答案 0 :(得分:1)

DocumentListener仅适用于Document的基础JTextComponent的通知。你需要DocumentFilter这里

答案 1 :(得分:0)

  

文档监听器不应修改文档的内容;在收听者收到更改通知时,更改已完成。而是编写一个自定义文档来覆盖insertString或删除方法,或两者​​兼而有之。有关详细信息,请参阅侦听文档的更改。

(来自http://docs.oracle.com/javase/tutorial/uiswing/events/documentlistener.html

我认为不是编写自定义文档,而是按照以下方式实现KeyListener更容易:

JTextField textField = new JTextField();
textField.addKeyListener( new KeyListener() {

@Override
public void keyTyped(KeyEvent arg0) {
    if(arg0.getKeyChar() == ' '){
        System.out.println("Space was entered! clearing the  text field");
        textField.setText("");
        arg0.consume();
    }

}

@Override
public void keyReleased(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void keyPressed(KeyEvent arg0) {
    // TODO Auto-generated method stub

}

清除文本字段后arg0.consume()阻止空格出现的位置