0

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?

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720

2 Answers2

2

"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());
            }
        });
    }
}
Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • tRnPy.getDocument().addDocumentListener(this); InputMap im = tRnPy.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); ActionMap am = tRnPy.getActionMap(); im.put(KeyStroke.getKeyStroke("ESCAPE"), CANCEL_ACTION); am.put(CANCEL_ACTION, new CancelAction()); – Yefta Wahyu Saputra Jul 02 '14 at 18:49
  • ` class CancelAction extends AbstractAction { public void actionPerformed(ActionEvent ev) { tRnPy.setText(""); } } private void cashBack(){ String paid = tRnPy.getText(); if(paid == "asu"){ lbRnCb.setText(paid); } } public void insertUpdate(DocumentEvent ev) { cashBack(); } public void removeUpdate(DocumentEvent ev) { cashBack(); } public void changedUpdate(DocumentEvent ev) { }` – Yefta Wahyu Saputra Jul 02 '14 at 18:51
  • All you need to do is add a new instance of DocumentListener, like `..addDocumentListener(new DocumentListener(){ // override methods });` – Paul Samsotha Jul 02 '14 at 18:53
  • See my **UPDATE** for a simple example – Paul Samsotha Jul 02 '14 at 18:59
  • I love you soo much... thanks.... it's totaly help me... hahaha... thanks.... ^_^ :* – Yefta Wahyu Saputra Jul 02 '14 at 19:11
0

JTextField inherits off off java.awt.Component which has a method http://docs.oracle.com/javase/8/docs/api/java/awt/Component.html#addKeyListener-java.awt.event.KeyListener- where you can register a listener and process what key is being pressed.

user3465651
  • 706
  • 1
  • 4
  • 22