I have a program with a JFrame
GUI and I want a button to be clicked when the user hits the Enter key from within a JTextField
. Yes I have tried
rootPane.setDefaultButton(jButton5);
but it only works when the text field isn't selected. How could I have it click the button when the text field is selected?
Thank you in advance :D
JTextField.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
jButton5.doClick();
}});
JTextField is you text field component. Then when you click the enter key, doClick method of JButton is invoked which Programmatically perform a "click". This does the same thing as if the user had pressed and released the button.
You'll have to add actionListener
to textField
and override method actionPerformed()
text.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Text=" + text.getText());
//Your logic
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.