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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:55:42+00:00 2026-06-17T19:55:42+00:00

The title is a bit ambiguous and I will explain in codes. Suppose I

  • 0

The title is a bit ambiguous and I will explain in codes. Suppose I have

Class A extends JFrame implements ActionListener{
     B b;
     Class B extends JPanel{
         public JButton button;
         public B(A a){
               button = new JButton();
               button.addActionListener(a);// I want to process all actionEvents in A
               this.add(button);
         }
     }
     public A(){
         b = new B(this);
         //irrelevant codes omitted for brevity  
     }

     public void actionPerformed(ActionEvent e){
         //Here's the question: 
         //Suppose I have a lot of Bs in A, 
         //how can I determine which B the button 
         //that triggers this callback belongs to?
     }
}

So is there any way to to that? Or my idea is wrong? Any thought is welcomed.

EDIT:
What I finally do is to add a function has(JComponent component) to B to compare against every clickable B has. The getParent() becomes awkward when you have multiple layers of JPanel as it’s hard to figure out which layer of panel it’s referring to and it’s against the idea of encapsulation.

  • 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-17T19:55:42+00:00Added an answer on June 17, 2026 at 7:55 pm

    Use e.getSource() to get a reference to the exact component that triggered the event. In your case, it will be a JButton. To get the panel it sits on, use e.getSource().getParent().

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

Sidebar

Related Questions

Yes, title sounds a bit confusing, so I'll explain what I mean: suppose you
Bit long title, comes down to this: <?php class error { public function error($errormsg
Forgive me if the title is a bit vague. I will try to explain
The title is a bit confusing...I'll explain I have an NSMutableArray I am populating
Ok... so i guess the title is a bit confusing. so i will explain:
The title may be a bit ambiguous, but I couldn't think of a better
The title is a bit confusing but here's what I'm after: I have a
So I admit the title is a bit, confusing, I apologize cause I have
Sorry if the title is a bit unclear/ambiguous but I am unsure of how
The title might be ambiguous, didn't know how else to word it. I have

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.