Search code examples
javaswingawtjtextfielddocumentlistener

Java Listener not working unless i press enter how to enable it working once text box changed


I have sticked below listener inside JtextField when action performed event, so as to perform action against any change made in text box once user make any change. but the problem is the code not starting or working unless you press enter only then the code execute the code, i need to know what i have to add and where to enable below code once text Filed changed instantly .I can see some similar help referring to Oracle listener help but am unable to manage so i need direct simple way.

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
jTextField1.getDocument().addDocumentListener(new DocumentListener() {

public void changedUpdate(DocumentEvent e) {
JOptionPane.showMessageDialog(null, "Change case");
} 
public void insertUpdate(DocumentEvent de) {
    JOptionPane.showMessageDialog(null, "Update Case");
}

public void removeUpdate(DocumentEvent de) {
  JOptionPane.showMessageDialog(null, "Remove case");
 }
});
 // TODO add your handling code here:
}                                           

Solution

  • ActionListener for a text field only listens for enter key being typed. So what your code essentialy does is: when enter key is pressed adds a new DocumentListener to the text field.

    The DocumentListener is what you want so take that code (adding the document listener) out of the jTextField1ActionPerformed method and put it in the constructor of the class. Or have a private method, so as not to clutter the constructor.

    Assuming you are using Netbeans GUI editor (looks like it from your method signature):

    public class MyFrame exentds JFrame {
        public MyFrame() {
            initComponents();
            addDocumentListenerToField();
        }
    
        private void addDocumentListenerToField() {
            jTextField.getDocument().addDocumentListener(..);
        }
    }
    

    UPDATE: DEMO

    import java.awt.GridLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JTextField;
    import javax.swing.SwingConstants;
    import javax.swing.SwingUtilities;
    import javax.swing.event.DocumentEvent;
    import javax.swing.event.DocumentListener;
    
    public class DocListeenerDemo extends JFrame {
        private JTextField field;   
        private JLabel label;
    
        public DocListeenerDemo() {
            initComponents();
            addDocumentListenerToField();
        }
    
        private void initComponents() {
            setLayout(new GridLayout(0, 1));
            field = new JTextField(20);
            label = new JLabel("", SwingConstants.CENTER);
            add(field);
            add(label);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            pack();
            setLocationByPlatform(true);
        }
    
        private void addDocumentListenerToField() {
            field.getDocument().addDocumentListener(new DocumentListener(){
                public void changedUpdate(DocumentEvent arg0) {doYourStuff();}
                public void insertUpdate(DocumentEvent arg0) {doYourStuff();}
                public void removeUpdate(DocumentEvent arg0) {doYourStuff();}
                public void doYourStuff() {
                    label.setText(field.getText());
                }
            });
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable(){
                public void run() {
                    DocListeenerDemo demo = new DocListeenerDemo();
                    demo.setVisible(true);
                }
            });
        }
    }
    

    I have not figure out a way to add the DocumentListener through the GUI tool. Sucks.