Ofer Gozlan
Ofer Gozlan

Reputation: 1143

Changing the content of jTextField while typing

I'm trying to reset my JTextField in case that Space is typed. It throws me:

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

my code is:

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

Upvotes: 1

Views: 1833

Answers (2)

Reimeus
Reimeus

Reputation: 159874

A DocumentListener is only suitable for notifications to the underlying Document of a JTextComponent. You need a DocumentFilter here

Upvotes: 1

t_over
t_over

Reputation: 122

Document listeners should not modify the contents of the document; The change is already complete by the time the listener is notified of the change. Instead, write a custom document that overrides the insertString or remove methods, or both. See Listening for Changes on a Document for details.

(From http://docs.oracle.com/javase/tutorial/uiswing/events/documentlistener.html)

Instead of writing a custom document, I believe it's easier to implement a KeyListener as followed:

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

}

Where the arg0.consume() prevents the space from appearing after clearing the textfield

Upvotes: 0

Related Questions