--EDIT--
私は JTextField を持っており、誰かが JTextField 内の文字を変更 (入力または削除) するたびに、メソッド (今は print ステートメントだけにします) を呼び出す必要があります。その背後にある目的は、入力されたものが特定の条件を満たすかどうかをメソッドが即座にチェックすることです。あなたの助けのおかげで、私は書くことができました:
public class MyDocumentListener implements DocumentListener {
public void insertUpdate(DocumentEvent e) {
updateLog(e, "inserted into");
}
public void removeUpdate(DocumentEvent e) {
updateLog(e, "removed from");
}
public void changedUpdate(DocumentEvent e) {
//Plain text components do not fire these events
}
public void updateLog(DocumentEvent e, String action) {
System.out.println("should call the method here");
}
}
JTextField コード:
JTextField passwordField = new JTextField();
passwordField.getDocument().addDocumentListener(new MyDocumentListener());
passwordField.getDocument().putProperty("name", "Text Field");
私が今得ている問題は、使用する必要があるということです
String textFieldPassword = passwordField.getText();
しかし、それは戻りますNullPointerException
。DocumentListener
これは、 を追加したためであり、現在動作している必要があると想定していDocumentEvent
ます。しかし、私はそれを行う方法が本当にわかりません。