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 8853527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:40:35+00:00 2026-06-14T13:40:35+00:00

In java, can I change the reference to a listener after the object is

  • 0

In java, can I change the reference to a listener after the object is constructed?
for example, Can I change the listener using its setter when an object of this class is instantiated?
If I can’t, how can I do that, I mean changing the listener whenever I needed?

public class ListenerTest extends JFrame {

    ActionListener listener;

    public ListenerTest() {

        JPanel jPanel = new JPanel();
        JButton jButton = new JButton("Activate!");
        jButton.addActionListener(listener);
        jPanel.add(jButton);
        add(jPanel);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("Demo Drawing");
        setLocationRelativeTo(null);
        pack();
        setVisible(true);
    }

    public ActionListener getListener() {
        return listener;
    }

    public void setListener(ActionListener listener) {
        this.listener = listener;

    }

    public static void main(String[] args) {
        ListenerTest frame = new ListenerTest();
    }

}
  • 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-14T13:40:36+00:00Added an answer on June 14, 2026 at 1:40 pm

    Sure you can add, remove ActionListeners, but not as you’re trying. If you change the ActionListener referenced to by the listener variable, this will have no effect on the one that has been added to the JButton. You must specifically add or remove listeners to the JButton via its addActionListener(...) and removeActionListener(...) methods to have this effect. I think that the key point that you need to understand is that the listener variable is not the same as the ActionListener object that it might refer to. All this variable does is refer to an ActionListener object if one has been given to it. It has absolutely no effect on the ActionListener that may or may not be listening to the JButton.

    By the way, your current code appears to be trying to add null as the JButton’s ActionListener since it the listener variable null at the time it is added to the button in the class’s constructor:

    ActionListener listener; // variable is null here
    
    public ListenerTest() {
    
        JPanel jPanel = new JPanel();
        JButton jButton = new JButton("Activate!");
        jButton.addActionListener(listener); // variable is still null here!
        // ....
    }
    
    public void setListener(ActionListener listener) {
        this.listener = listener;  // this has no effect on the JButton
    }
    

    Perhaps instead you want to do this:

    public void setListener(ActionListener listener) {
        jButton.addActionListener(listener);
    }
    

    or if you want your listener added in place of all existing ActionListeners

    public void setListener(ActionListener listener) {
        ActionListener[] listeners = jButton.getActionListeners();
        for(ActionListener l : listeners) {
           jButton.removeActionListener(l); // remove all current ActionListeners
        }
        // set new ActionListener
        jButton.addActionListener(listener);
    }
    

    You also can set the JButton’s Action if you prefer to use AbstractActions, and some feel that this is a cleaner way to go about this.

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

Sidebar

Related Questions

How can I change the current working directory from within a Java program? Everything
What is the maximum file size 32 bit java can access? Is this architecture
My program needs to represent this date as a java.sql.date object , but it
I know java script doesn't have call by reference. So how can I solve
In Java, can one force a method to return by value (not by reference)?
Possible Duplicate: Is Java “pass-by-reference”? I want to know how to pass an object
How can i get hold of the instantiating object from a constructor in java?
Can I pass a string from Java to my C++ routine using JNI function
I am having a session variable whose value can be changed from java and
I'm looking for a Java library which can notify me about changes on the

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.