Call actionPerformed Method using normal method of class

12,643

Solution 1

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();
            }
        });
    }
}

Solution 2

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()
}
Share:
12,643
Vighanesh Gursale
Author by

Vighanesh Gursale

Updated on June 04, 2022

Comments

  • Vighanesh Gursale
    Vighanesh Gursale almost 2 years

    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

  • trashgod
    trashgod about 11 years
    +1 for doClick(), also seen here with key bindings.
  • nIcE cOw
    nIcE cOw almost 11 years
    @VighaneshGursale : You're MOST WELCOME and KEEP SMILING :-)
  • nIcE cOw
    nIcE cOw over 9 years
    This one is no doubt, a good approach. More modular based, I like it +1 :-)