Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6896533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:03:20+00:00 2026-05-27T07:03:20+00:00

I have ActionListener actionListenerRing = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { defined

  • 0

I have

ActionListener actionListenerRing = new ActionListener() {
              public void actionPerformed(ActionEvent actionEvent) {

defined in my class constructor. How to call actionPerformed method form main method?

I just wanted to know If i can call action listener directly form any other part of my class, OR I should move code form action listener to other method to be accessible to both ActioListener AND other class methods.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:03:21+00:00Added an answer on May 27, 2026 at 7:03 am

    Typically, you do not create a listener without attaching it to something. So if your main method must call such an ActionListener directly something might be wrong with your design. Perhaps you can comment on what you try to achieve.

    You might want to read the whole observer design pattern, but in short you add your listener to another object if you want to be informed when it performs an action. For example:

    JButton button;//a button I obtained from somewhere and in 
    // which I am interested
    button.addActionListener( new ActionListener(){ 
      public void actionPerformed( ActionEvent actionEvent ){
         //do something with the fact that the button has been pressed
      }
    });
    

    In the above example, the button will inform the ActionListener I added when it’s pressed, and I can react on that by placing the relevant code in the actionPerformed method. But I am not going to call my listener myself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class saveButtonListener implements ActionListener{ public void actionPerformed(ActionEvent ev){ JFileChooser chooser= new JFileChooser(); String
I have the following two buttons: jButton2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) {
I have a code right below...take a look. enter.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent
class SomeClass implements ActionListener { public void actionPerformed(ActionEvent e) { // do something }
I have a class like this: public class A { public static void main()
I have the following code: public class Test extends JFrame implements ActionListener{ private static
I have something like the code below: for(int i=0;i<10;i++){ button=new JButton(buttons[i]); button.addActionListener(new ActionListener(){ public
I have a class SaveFile implementing ActionListener. The method in it takes a string
I have this line of code. class ButtonPanel extends JPanel implements ActionListener { public
I have this timer here: timer = new Timer(delay, new ActionListener() { @Override public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.