以编程方式触发JTextField中的键事件?

ktu*_*nho 2 java swing keyevent jtextfield

如何以编程方式触发JTextField正在侦听事件的按键事件ENTER

我的关键事件的监听JTextField器声明如下:

myTextField.addKeyListener(new KeyAdapter() {

    @Override
    public void keyTyped(KeyEvent e) {
        if (e.getKeyChar() == KeyEvent.VK_ENTER) {
            // Do stuff
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

谢谢.

Dav*_*amp 13

  • Do not use KeyListener on JTextField simply add ActionListener which will be triggered when ENTER is pressed (thank you @robin +1 for advice)

    JTextField textField = new JTextField();
    
    textField.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
             //do stuff here when enter pressed
        }
    });
    
    Run Code Online (Sandbox Code Playgroud)
  • To trigger KeyEvent use requestFocusInWindow() on component and use Robot class to simulate key press

Like so:

textField.requestFocusInWindow();

try { 
    Robot robot = new Robot(); 

    robot.keyPress(KeyEvent.VK_ENTER); 
} catch (AWTException e) { 
e.printStackTrace(); 
} 
Run Code Online (Sandbox Code Playgroud)

Example:

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Test {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JTextField textField = new JTextField();

                textField.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        System.out.println("Here..");
                    }
                });
                frame.add(textField);

                frame.pack();
                frame.setVisible(true);

                textField.requestFocusInWindow();

                try {
                    Robot robot = new Robot();

                    robot.keyPress(KeyEvent.VK_ENTER);
                } catch (AWTException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

UPDATE:

As others like @Robin and @mKorbel have suggested you might want a DocumentListener/DocumentFiler (Filter allows validation before JTextField is updated).

You will need this in the event of data validation IMO.

see this similar question here

it shows how to add a DocumentFilter to a JTextField for data validation. The reason for document filter is as I said allows validation before chnage is shown which is more useful IMO

  • -1已经存在ENTER键的绑定.它将触发附加到文本字段的`ActionListener`.覆盖这可能会导致细微问题 (2认同)
  • `我如何在JTextField上触发按键事件有问题:-)我打赌这只是关于DocumentListener,甚至ActionListner返回整个Document (2认同)