In my program have a JButton and JTextField.
I want jButton fire while press Enter key in JTextField
I'm not talking about KeyEvent.
private void jTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
this.getRootPane().setDefaultButton(jButton1);
}
This just make JButton focus enabled after enter in JTextField
So when I will enter in JTextField it must fire JButton ActionEvent.
And have this code on jButton1 ActionPerformed method.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane.showMessageDialog(rootPane, "Hi!");
}
So how can I make JButton fired on JTextField ActionEvent?
I tried following answer but in IDE it throwing exception.
Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: javax.swing.JButton.addActionListener
One way to achieve that (if i got this straight), is to add an
ActionListenerto the JTextField which will callJButton#doClick()method. (I think it is pretty clear what thedoClick()method does).An example: