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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:26:46+00:00 2026-05-31T08:26:46+00:00

I have three classes, one main class, one GUI class which uses awt+swing to

  • 0

I have three classes, one main class, one GUI class which uses awt+swing to make a basic window with 4 buttons.

//BEGIN ADD ACTION LISTENERS
handle_events event_handler=new handle_events();
home_b.addActionListener(event_handler);
my_account_b.addActionListener(event_handler);
my_history_b.addActionListener(event_handler);
exit_b.addActionListener(event_handler);
//END ADD ACTION LISTENERS

My handle_events class looks like this:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class handle_events implements ActionListener
{
    public handle_events(){}

    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource==home_b) {do stuff;}
            //etc               
    }


}
//END EVENT HANDLER CLASS

The problem is that home_b can’t be found by the ActionListener class, regardless of what I do. Thanks for all your help.

  • 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-31T08:26:48+00:00Added an answer on May 31, 2026 at 8:26 am

    Because your handle_events class is in another scope it will never find the home_b variable. That’s the reason a lot people use Anonymous Listener classes for event handlers.

    JButton button = new JButton((new AbstractAction("name of button") {
    public void actionPerformed(ActionEvent e) {
        //do stuff here
        }
    }));
    

    The biggest benefit to doing it this way is that you don’t need to check to see who is the source, you know it right then and there in the code what that handler needs to do.

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

Sidebar

Related Questions

I have Three different classes that all need to use instances of one class.
I have one main class main, which I use to store the configuration and
I have three classes; Classes A and B both reference class C . How
Say I have three classes: class X{}; class Y{}; class Both : public X,
If I have three classes class A class B extends A class C extends
I have a class hierarchy with the following three classes: template<int pdim > class
I have a WPF application with the main Window class called MainWindow. Since I
I have a base class that three classes inherit from. Whenever these child classes
I have two classes - one that runs in the main thread and performs
I have three projects under another project of which each contains one namespace. Now

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.