1

i am trying to trigger key released event in java swing.

txtEmailId.addKeyListener(new KeyAdapter() {
            // override keyReleased listener on the Email TextField
            @Override
            public void keyReleased(KeyEvent e) {

                                System.out.println("ok");
            }
        });

above code running successfully now i want to call this keyReleased event after txtEmailId.setText("hello"); manually how it is possible?

6
  • 1
    you means how to trigger the keyReleased by code manually? Commented Apr 2, 2014 at 6:55
  • yes ,how it is possible? Commented Apr 2, 2014 at 6:56
  • You cannot call keyReleased programatically, please check Documentation Commented Apr 2, 2014 at 6:59
  • Then how i can solve my problem? is there any way? Commented Apr 2, 2014 at 7:03
  • 1) For Swing, typically use key bindings over the AWT based, lower level, KeyListener. See How to Use Key Bindings for details on how to use them. 2) This is a classic XY problem. Commented Apr 2, 2014 at 7:20

1 Answer 1

2

Instead of KeyAdapter use DocumentListener. Try next example:

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;

public class TestFrame extends JFrame{

    public TestFrame(){
        init();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    private void init() {
        JTextField f = new JTextField();
        f.getDocument().addDocumentListener(new DocumentListener() {

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

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

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

            private void validate(DocumentEvent e) {
                try {
                    String text = e.getDocument().getText(0, e.getDocument().getLength());
                    if(text.equals("hello")){
                        System.out.println("ok");
                    }
                } catch (BadLocationException e1) {
                    e1.printStackTrace();
                }
            }
        });
        f.setText("hello");
        add(f);
    }

    public static void main(String... s){
        new TestFrame();
    }

}
Sign up to request clarification or add additional context in comments.

6 Comments

how this is meet with my question?
I suggest you another way for triggering event on your JTextField without fire events manually.
@user3363563 It is not-recommended to use a KeyListener of text components as the events could be consumed before they reach your listener or cause mutation errors if you try and modify the field during the key event notification, it also does not take into account when text is lasted into the field. DocumentListener is the recommended ethos for monitoring changes to fields/documents content and DocumentFilter for filtering content before its committed to the document/field
@user3363563 This solution solves your problem directly as it will be notified when setText is called, all round, it's an excellent solution for your problem as it overcomes a number of issues you're not having yet and the problem you are having and dose not deserve a vote down. +1
Well, since it seems like you asked an XY problem, it seems this answer was trying to guess the point of what you are doing, and provide an altogether better approach to it. It doesn't take much experience to know that anything that mixes Swing and KeyListener has a better solution, but it is hard to know what the best solution is, until you state what you are trying to achieve (as opposed to what you are trying to do).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.