简体   繁体   中英

JTextField event listener

using Visual Basic to determine when TextField filled. with TextField.Change ().

use the JavaScript to know when TextField filled. with onkeyup.

how can I do the operation when user was filling my JTextField? if in java, when JButton in the press I use "JButtonActionPerformed". Jtable when clicked, I use "JTableMouseClicked".

the event what should I use. for JTextField being written?

"what should I use. for JTextField being written?"

Use a DocumentListener that listens for changes in the underlying document of the text field

Depending on what you're trying to do, if it's real time validating, you may want to look into using a DocumentFilter instead. See examples here


UPDATE

Here's a simple example using a DocumentListener

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class DocumentListenerDemo {

    public static JTextField getTextField() {
        final JTextField field = new JTextField(10);
        field.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void removeUpdate(DocumentEvent e) {
                System.out.println(field.getText());    
            }
            @Override
            public void insertUpdate(DocumentEvent e) {
                System.out.println(field.getText());
            }
            @Override
            public void changedUpdate(DocumentEvent e) {}
        });
        return field;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                JOptionPane.showMessageDialog(null, getTextField());
            }
        });
    }
}

JTextField继承自具有方法http://docs.oracle.com/javase/8/docs/api/java/awt/Component.html#addKeyListener-java.awt.event.KeyListener-的 java.awt.Component。您可以注册一个侦听器并处理所按下的键。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM