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?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Uday A. Navapara
  • 1,237
  • 5
  • 20
  • 40
  • 1
    you means how to trigger the keyReleased by code manually? –  Apr 02 '14 at 06:55
  • yes ,how it is possible? – Uday A. Navapara Apr 02 '14 at 06:56
  • You cannot call keyReleased programatically, please check [Documentation](http://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyListener.html#keyReleased(java.awt.event.KeyEvent)) – Infinite Recursion Apr 02 '14 at 06:59
  • Then how i can solve my problem? is there any way? – Uday A. Navapara Apr 02 '14 at 07:03
  • 1) For Swing, typically use key bindings over the AWT based, lower level, `KeyListener`. See [How to Use Key Bindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) for details on how to use them. 2) This is a classic [XY problem](http://meta.stackexchange.com/q/66377/155831). – Andrew Thompson Apr 02 '14 at 07:20
  • Use a DocumentListener as it will be noticed when setText is called, when the user types someone or text is pasted into te field, won't suffer from possible mutation exceptions – MadProgrammer Apr 02 '14 at 07:23

1 Answers1

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();
    }

}
alex2410
  • 10,904
  • 3
  • 25
  • 41
  • how this is meet with my question? – Uday A. Navapara Apr 02 '14 at 07:01
  • I suggest you another way for triggering event on your `JTextField` without fire events manually. – alex2410 Apr 02 '14 at 07:05
  • 1
    @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 – MadProgrammer Apr 02 '14 at 07:18
  • @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 – MadProgrammer Apr 02 '14 at 07:21
  • 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*). – Andrew Thompson Apr 02 '14 at 07:22
  • ok now? it's my mistake because i am beginner in java i have accept your answer now – Uday A. Navapara Apr 02 '14 at 07:33