值更改侦听器到JTextField

use*_*501 207 java swing listener jtextfield documentlistener

我希望在用户更改文本字段中的值后立即显示消息框.目前,我需要按Enter键才能弹出消息框.我的代码有什么问题吗?

textField.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent e) {

        if (Integer.parseInt(textField.getText())<=0){
            JOptionPane.showMessageDialog(null,
                    "Error: Please enter number bigger than 0", "Error Message",
                    JOptionPane.ERROR_MESSAGE);
        }       
    }
}
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激!

Cod*_*nci 363

向基础文档添加一个监听器,该监听器会自动为您创建.

// Listen for changes in the text
textField.getDocument().addDocumentListener(new DocumentListener() {
  public void changedUpdate(DocumentEvent e) {
    warn();
  }
  public void removeUpdate(DocumentEvent e) {
    warn();
  }
  public void insertUpdate(DocumentEvent e) {
    warn();
  }

  public void warn() {
     if (Integer.parseInt(textField.getText())<=0){
       JOptionPane.showMessageDialog(null,
          "Error: Please enter number bigger than 0", "Error Message",
          JOptionPane.ERROR_MESSAGE);
     }
  }
});
Run Code Online (Sandbox Code Playgroud)

  • "错误按摩" (12认同)

Boa*_*ann 46

通常的答案是"使用DocumentListener".但是,我总觉得界面繁琐.说实话,界面过度设计了.它有三种方法,用于插入,删除和替换文本,只需要一种方法:替换.(插入可以被视为用一些文本替换没有文本,并且可以将删除视为一些没有文本的文本的替换.)

通常你只想知道框中的文本何时发生了变化,所以典型的DocumentListener实现有三种方法调用一种方法.

因此我制作了以下实用程序方法,它允许您使用更简单ChangeListener而不是DocumentListener.(它使用Java 8的lambda语法,但如果需要,您可以根据旧Java进行调整.)

/**
 * Installs a listener to receive notification when the text of any
 * {@code JTextComponent} is changed. Internally, it installs a
 * {@link DocumentListener} on the text component's {@link Document},
 * and a {@link PropertyChangeListener} on the text component to detect
 * if the {@code Document} itself is replaced.
 * 
 * @param text any text component, such as a {@link JTextField}
 *        or {@link JTextArea}
 * @param changeListener a listener to receieve {@link ChangeEvent}s
 *        when the text is changed; the source object for the events
 *        will be the text component
 * @throws NullPointerException if either parameter is null
 */
public static void addChangeListener(JTextComponent text, ChangeListener changeListener) {
    Objects.requireNonNull(text);
    Objects.requireNonNull(changeListener);
    DocumentListener dl = new DocumentListener() {
        private int lastChange = 0, lastNotifiedChange = 0;

        @Override
        public void insertUpdate(DocumentEvent e) {
            changedUpdate(e);
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            changedUpdate(e);
        }

        @Override
        public void changedUpdate(DocumentEvent e) {
            lastChange++;
            SwingUtilities.invokeLater(() -> {
                if (lastNotifiedChange != lastChange) {
                    lastNotifiedChange = lastChange;
                    changeListener.stateChanged(new ChangeEvent(text));
                }
            });
        }
    };
    text.addPropertyChangeListener("document", (PropertyChangeEvent e) -> {
        Document d1 = (Document)e.getOldValue();
        Document d2 = (Document)e.getNewValue();
        if (d1 != null) d1.removeDocumentListener(dl);
        if (d2 != null) d2.addDocumentListener(dl);
        dl.changedUpdate(null);
    });
    Document d = text.getDocument();
    if (d != null) d.addDocumentListener(dl);
}
Run Code Online (Sandbox Code Playgroud)

与直接向文档添加侦听器不同,它处理在文本组件上安装新文档对象的(不常见)情况.此外,它解决了Jean-Marc Astesana的答案中提到的问题,其中文档有时会触发更多事件而不是它需要的事件.

无论如何,这种方法可以让你替换烦人的代码,如下所示:

someTextBox.getDocument().addDocumentListener(new DocumentListener() {
    @Override
    public void insertUpdate(DocumentEvent e) {
        doSomething();
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        doSomething();
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        doSomething();
    }
});
Run Code Online (Sandbox Code Playgroud)

附:

addChangeListener(someTextBox, e -> doSomething());
Run Code Online (Sandbox Code Playgroud)

代码发布到公共领域.玩得开心!

  • 类似的解决方案:使用一个额外的抽象方法`change(DocumentEvent e)`创建一个`抽象类DocumentChangeListener实现DocumentListener`,你可以从所有其他3个方法调用它.对我来说似乎更明显,因为它使用与`abstract*Adapter`听众大致相同的逻辑. (3认同)

Jea*_*ana 15

请注意,当用户修改字段时,DocumentListener有时可以接收两个事件.例如,如果用户选择整个字段内容,则按一个键,您将收到removeUpdate(所有内容都被删除)和insertUpdate.在你的情况下,我不认为这是一个问题,但一般来说,它是.不幸的是,似乎没有办法在没有继承JTextField的情况下跟踪textField的内容.以下是提供"text"属性的类的代码:

package net.yapbam.gui.widget;

import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/** A JTextField with a property that maps its text.
 * <br>I've found no way to track efficiently the modifications of the text of a JTextField ... so I developed this widget.
 * <br>DocumentListeners are intended to do it, unfortunately, when a text is replace in a field, the listener receive two events:<ol>
 * <li>One when the replaced text is removed.</li>
 * <li>One when the replacing text is inserted</li>
 * </ul>
 * The first event is ... simply absolutely misleading, it corresponds to a value that the text never had.
 * <br>Anoter problem with DocumentListener is that you can't modify the text into it (it throws IllegalStateException).
 * <br><br>Another way was to use KeyListeners ... but some key events are throw a long time (probably the key auto-repeat interval)
 * after the key was released. And others events (for example a click on an OK button) may occurs before the listener is informed of the change.
 * <br><br>This widget guarantees that no "ghost" property change is thrown !
 * @author Jean-Marc Astesana
 * <BR>License : GPL v3
 */

public class CoolJTextField extends JTextField {
    private static final long serialVersionUID = 1L;

    public static final String TEXT_PROPERTY = "text";

    public CoolJTextField() {
        this(0);
    }

    public CoolJTextField(int nbColumns) {
        super("", nbColumns);
        this.setDocument(new MyDocument());
    }

    @SuppressWarnings("serial")
    private class MyDocument extends PlainDocument {
        private boolean ignoreEvents = false;

        @Override
        public void replace(int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
            String oldValue = CoolJTextField.this.getText();
            this.ignoreEvents = true;
            super.replace(offset, length, text, attrs);
            this.ignoreEvents = false;
            String newValue = CoolJTextField.this.getText();
            if (!oldValue.equals(newValue)) CoolJTextField.this.firePropertyChange(TEXT_PROPERTY, oldValue, newValue);
        }

        @Override
        public void remove(int offs, int len) throws BadLocationException {
            String oldValue = CoolJTextField.this.getText();
            super.remove(offs, len);
            String newValue = CoolJTextField.this.getText();
            if (!ignoreEvents && !oldValue.equals(newValue)) CoolJTextField.this.firePropertyChange(TEXT_PROPERTY, oldValue, newValue);
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • Swing已经_has_一种textField,它将文档更改映射到属性 - 它被称为JFormattedTextField :-) (3认同)

And*_*nov 14

只需创建一个扩展DocumentListener并实现所有DocumentListener方法的接口:

@FunctionalInterface
public interface SimpleDocumentListener extends DocumentListener {
    void update(DocumentEvent e);

    @Override
    default void insertUpdate(DocumentEvent e) {
        update(e);
    }
    @Override
    default void removeUpdate(DocumentEvent e) {
        update(e);
    }
    @Override
    default void changedUpdate(DocumentEvent e) {
        update(e);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后:

jTextField.getDocument().addDocumentListener(new SimpleDocumentListener() {
    @Override
    public void update(DocumentEvent e) {
        // Your code here
    }
});
Run Code Online (Sandbox Code Playgroud)

或者您甚至可以使用lambda表达式:

jTextField.getDocument().addDocumentListener((SimpleDocumentListener) e -> {
    // Your code here
});
Run Code Online (Sandbox Code Playgroud)

  • 不要忘记,在 Java 8 之前的所有版本中,此解决方案都需要抽象类而不是接口。 (2认同)

Ast*_*dax 11

我知道这与一个非常古老的问题有关,但是,它也给我带来了一些问题.正如kleopatra在上面的评论中回应,我解决了问题JFormattedTextField.但是,该解决方案需要更多的工作,但更整洁.

JFormattedTextField默认情况下不会引发在该领域的每个文本更改后的属性变化.默认构造函数JFormattedTextField不会创建格式化程序.

但是,要执行OP建议的操作,您需要使用格式化程序,该格式化程序将commitEdit()在每次有效编辑字段后调用该方法.该commitEdit()方法触发了我所看到的属性更改,并且没有格式化程序,默认情况下会在焦点更改或按下回车键时触发.

有关详细信息,请参阅http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html#value.

创建一个默认的formatter(DefaultFormatter)对象,JFormattedTextField通过其构造函数或setter方法传递给它们.默认格式化程序的一种方法是setCommitsOnValidEdit(boolean commit)设置格式化程序,以便在commitEdit()每次更改文本时触发该方法.然后可以使用a PropertyChangeListenerpropertyChange()方法拾取它.


mac*_*e.k 8

一种优雅的方法是将侦听器添加到插入符号位置,因为每次键入/删除某些内容时它都会发生变化,然后只需将旧值与当前值进行比较即可。

String oldVal = ""; // empty string or default value
JTextField tf = new JTextField(oldVal);

tf.addCaretListener(e -> {
    String currentVal = tf.getText();

    if(!currentVal.equals(oldVal)) {
        oldVal = currentVal;
        System.out.println("Change"); // do something
    }
});
Run Code Online (Sandbox Code Playgroud)

(每次用户单击文本字段时也会触发此事件)。