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

  • SEARCH
  • Home
  • 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 7847363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:52:14+00:00 2026-06-02T17:52:14+00:00

I just started getting my head around Java itself and Java Swing and I

  • 0

I just started getting my head around Java itself and Java Swing and I have some problems understanding the “Action Listener” concept. People say that C# and Java is very alike, but that’s another story when you actually try out both of them and compare.

I have the following auto-generated Action Listener for a button:

btnNewButton.addActionListener(new ActionListener() 
{
    public void actionPerformed(ActionEvent e) 
    {
        lblNylabel.setText("New label text");
    }
});

I understand it like this:

  1. You call a non-static method via the object “btnNewButton” btnNewButton.addActionListener()
  2. The method takes one ActionListener instance as an argument
  3. The automated code instansiates an ActionListener instance via the “new ActionListener()” constructor call – What I don’t understand is that I can’t instansiate the ActionListener class myself, but it’s possible as an argument in the method call??
  4. A “actionPerformed” method is generated inside the new instance body and used here (What?)
  5. Inside the “actionPerformed” method you define what to do, when the button is clicked – Makes perfectly sense

Is it possible to do this in a more understanding/simple way that could help me understand the ActionListener concept?

  • 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-06-02T17:52:16+00:00Added an answer on June 2, 2026 at 5:52 pm

    What I don’t understand is that I can’t instansiate the ActionListener class myself, but it’s possible as an argument in the method call??

    When you do

    new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            lblNylabel.setText("New label text");
       }
    }
    

    You’re actually creating an instance of an anonymous subclass of ActionListener.

    It is semantically equivalent of doing

    class AnonymousActionListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            lblNylabel.setText("New label text");
       }
    }
    
    btnNewButton.addActionListener(new AnonymousActionListener());
    

    (And tada, as a bonus, you just learned that you can have method local classes in Java 😉


    Here are a few common alternatives:

    1. Use an separate ordinary class:

      class MyActionListener implements ActionListener {
          public void actionPerformed(ActionEvent e) {
              lblNylabel.setText("New label text");
          }
      }
      
      class YourClass {
      
          public void yourMethod() {
              ...
              btnNewButton.addActionListener(new MyActionListener());
          }
      }
      

      (only possible if the other class has access to the required variables.)

    2. Same as above, but with an inner (non-static) class:

      class YourClass {
      
          public void yourMethod() {
              ...
              btnNewButton.addActionListener(new MyActionListener());
          }
      
          // Inner class
          class MyActionListener implements ActionListener {
              public void actionPerformed(ActionEvent e) {
                  lblNylabel.setText("New label text");
              }
          }
      }
      

      (Here lblNylabel will probably be in scope for the inner class.)

    3. Let the enclosing class itself implement the ActionListener and use this as argument to addActionListener:

      class YourClass implements ActionListener {
      
          public void yourMethod() {
              ...
              btnNewButton.addActionListener(this);
          }
      
          public void actionPerformed(ActionEvent e) {
              lblNylabel.setText("New label text");
          }
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am just getting started with NuGet and trying to get my head around
I've just started using Git (previously Subversion). I'm having real problems getting my head
I just started getting into iPhone development. I have been mish-mashing tutorials and material
I have just started getting a very strange error when using jQueryMobile for my
Right guys, I have just started getting into Xcode 4, with the intention to
Just started to getting familiar with graphics in Java. What is the simplest way
I am just getting started with HTML/JavaScript, and I have a simple question. I
I'm building a site, and have just started getting into developing the backend. The
I'm just getting started with subversion. I have web applications which I need to
Just getting started using MVC in ASP.NET, I'm going to have it so users

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.