Jisson
Jisson

Reputation: 3725

Execute the same code in action performed of a button, when click enter from a textfield

I have a class extending JDialog. In that i place a JTextField and a JButton (jButton1). There is some code inside jButton1ActionPerformed.

I want to execute the same code when I enter some values in the JTextField and click enter button.

Upvotes: 0

Views: 1147

Answers (2)

StanislavL
StanislavL

Reputation: 57381

jButton1.doClick();

you can call it

Upvotes: 0

Qwerky
Qwerky

Reputation: 18405

Have your class implement ActionListener and put your code inside the actionPerformed method. You can then set the instance to be the actionListener on both the button and text field.

public MyDialog extends JDialog implements ActionListener
{
  JTextField myTextField;
  JButton myButton;

  public MyDialog()
  {
    //set up and add components here

    myButton.addActionListener(this);
    myTextField.addActionListner(this);
  }

  public void actionPerformed(ActionEvent evt)
  {
    //code here that does stuff when button pressed, or enter pressed on text field
  }
}

Upvotes: 1

Related Questions