3

I am trying to call the actionPerformed() in normal method of class. I know that it get automatically executed on whenever button get pressed. But I want to call that method when ENTER button get pressed on Specific textfield. Is it possible to call actionPerformed() in keyPressed() or in normal function/method.

The following code will give you rough idea what I want to do.

void myFunction()
{
      actionPerformed(ActionEvent ae);
}

public void actionPerformed(ActionEvent ae)
{
      //my code
}

Thanks in advance

Vighanesh Gursale
  • 921
  • 5
  • 15
  • 31
  • 1
    you shouldn't do it if you want the execute method call then move your application logic to another method and call it processing events is a EDT job. – Roman C Apr 27 '13 at 09:56
  • 1
    What's wrong with just letting the `ActionListener` do it's job? This is the EXACT behaviour that `ActionListener` performs anyway – MadProgrammer Apr 27 '13 at 10:18

2 Answers2

5

If you want, some actionPerformed() method of a JButton to be executed on pressing ENTER inside a JTextField, then I guess you can use the doClick(), method from AbstractButton class to achieve this. Though this approach, might can override the original behaviour of the JTextField on press of the ENTER key :(

Please have a look at this code pasted below, to see if this is what, stands fit for your needs :-) !!!

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

public class ButtonClickExample
{
    private JTextField tfield;
    private JButton button;
    private JLabel label;

    private ActionListener actions = new ActionListener()
    {
        @Override
        public void actionPerformed(ActionEvent ae)
        {
            if (ae.getSource() == button)
            {
                label.setText(tfield.getText());
            }
            else if (ae.getSource() == tfield)
            {
                button.doClick();
            }
        }
    }; 

    private void displayGUI()
    {
        JFrame frame = new JFrame("Button Click Example");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(5, 5));

        JPanel centerPanel = new JPanel();
        tfield = new JTextField("", 10);
        button = new JButton("Click Me or not, YOUR WISH");
        tfield.addActionListener(actions);
        button.addActionListener(actions);
        centerPanel.add(tfield);
        centerPanel.add(button);
        contentPane.add(centerPanel, BorderLayout.CENTER);

        label = new JLabel("Nothing to show yet", JLabel.CENTER);
        contentPane.add(label, BorderLayout.PAGE_END);

        frame.setContentPane(contentPane);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                new ButtonClickExample().displayGUI();
            }
        });
    }
}
nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
3

I know this is an old thread, but for other people seeing this my recomendation is something like this:

// This calls the method that you call in the listener method
void performActionPerformedMethod(){
    actionPerformed(ActionEvent e);
}

// This is what you want the listener method to do
void actionPerformedMethod(){
// Code...
}

// This is the interface method
public void actionPerformed(ActionEvent e){
    actionPerformedMethod()
}
Janon
  • 91
  • 1
  • 8