Reputation: 3725
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
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