简体   繁体   中英

Fire Default Button on jTextField Action Performed

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 

You should NOT call getRootPane().setDefaultButton(jButton1); inside the ActionListener of text field (actually you don't need an ActionListener for text field).

You should call getRootPane().setDefaultButton(jButton1); in UI initialization.

setDefaultButton(jButton1) is NOT there to "fire" the Enter key event. It is there to "specify" that you want jButton1 to be the "default" button.

See below demonstration.

import javax.swing.*;
import java.awt.FlowLayout;

public class DefaultButton {

  public static void main(String[] args) {

    JButton button = new JButton("Button 1");
    button.addActionListener(DefaultButton::jButton1ActionPerformed);

    JTextField textField = new JTextField(20);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new FlowLayout());
    frame.getContentPane().add(button);
    frame.getContentPane().add(textField);

    frame.getRootPane().setDefaultButton(button);

    frame.setBounds(300, 200, 400, 300);
    frame.setVisible(true);
  }

  private static void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    JOptionPane.showMessageDialog(null, "Hi!");
  }
}

UPDATE:

If you don't want to make button the default button and you just want to show the message dialog when user hits Enter key, then you can call the same jButton1ActionPerformed() from an ActionListener of the text field like this:

JTextField textField = new JTextField(20);
textField.addActionListener(DefaultButton::jButton1ActionPerformed);

And remove this line:

frame.getRootPane().setDefaultButton(button);

One way to achieve that (if i got this straight), is to add an ActionListener to the JTextField which will call JButton#doClick() method. (I think it is pretty clear what the doClick() method does).

An example:

import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class FireButtonOnEnter extends JFrame {
    private static final long serialVersionUID = -7346953935931623335L;

    public FireButtonOnEnter() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(400, 400);
        setLocationRelativeTo(null);
        getContentPane().setLayout(new FlowLayout());
        JTextField textField = new JTextField(15);
        JButton button = new JButton("Print Hello");
        button.addActionListener(e -> System.out.println("Hello world."));
        textField.addActionListener(e -> button.doClick());
        getContentPane().add(textField);
        getContentPane().add(button);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new FireButtonOnEnter().setVisible(true));
    }
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM