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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:22:30+00:00 2026-05-23T23:22:30+00:00

First off – sorry for the wall of code but it’s not too horrendous,

  • 0

First off – sorry for the wall of code but it’s not too horrendous, just a framework for what I’m trying to explain. It runs without errors.

The goal

I’m making a reuseable button class for my GUI and each button object needs to have a different handler when it’s clicked. I want to to assign a ClickHandler object to each new button. Then, the button would call init() on the handler, and be on its way. Unfortunately, there’s a typing problem, since each handler class would have a different name.

Current progress

Right now, the handler is typed as HandlerA, but I’d like to have it handle any name, like “SettingsHandler” or “GoToTheWahWah” etc.

I’ve tried messing about with generic types, but since I’m new to this, and from a webdev background, there seems to be a conceptual hurdle I keep knocking over. Is this the right way to approach the problem?

The code

ReuseableButton.java is a reuseable class, the only thing that changes is the action when it’s clicked:

package gui;

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class ReuseableButton extends JButton implements ActionListener {
  private static final long serialVersionUID = 1L;

  // I want a generic type here, not just HandlerA!
  private HandlerA ClickHandler;

  // Assemble generic button
  public ReuseableButton(Container c, String s) {
    super(s);
    addActionListener(this);
    c.add(this);
  }

  // Once again, generic type, not just HandlerA!
  public void SetClickHandler(HandlerA ch) {
    ClickHandler = ch;
  }

  // Call init() from whatever class has been defined as click handler.
  public void actionPerformed(ActionEvent e) {
    ClickHandler.init();
  }
}

Controller.java fires the frame and assembles buttons as needed (right now, only one button).

package gui;
import javax.swing.*;
import java.awt.*;

public class Controller extends JFrame {
  private static final long serialVersionUID = 1L;


  public Controller() {
    JFrame frame = new JFrame("Handler Test GUI");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        

    Container pane = frame.getContentPane();   
    pane.setLayout(new FlowLayout());

    ReuseableButton b = new ReuseableButton(pane,"Reuseable Button A");
    // THE QUESTION IS HERE: Pass a generic object?
    b.SetClickHandler(new HandlerA());

    frame.pack();
    frame.setSize(200,200);    
    frame.setVisible(true);        
  }

  public static void main(String args[]) {
    new Controller();
  }
}

HandlerA.java is a sample of a random handler for the button click. Later, there could be HandlerB, HandlerC, etc.

package gui;

// A random handler
public class HandlerA {
  public void init() {
    System.out.println("Button clicked.");
  }
}

Thanks very much in advance!

  • 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-23T23:22:30+00:00Added an answer on May 23, 2026 at 11:22 pm

    I recommend to work with inheritence in this case:

    public abstract class AbstractHandler {
    
        public abstract void init();
    }
    

    Then:

    public class ConcreteHandlerA extends AbstractHandler {
    
        @Override
        public void init() {
            // do stuff...
        }
    
    }
    

    Controller

    public class ReuseableButton extends JButton implements ActionListener {
        // I want a generic type here, not just HandlerA!
        private AbstractHandler ClickHandler;
    
        public Controller() {
            //...
    
            ReuseableButton b = new ReuseableButton(pane,"Reuseable Button A");
            AbstractHandler handlerA = new ConcreteHandlerA();
            b.SetClickHandler(handlerA);
    
            // ...
        }
    }
    

    Not sure if this is what you’re looking for…

    BTW: You can define the AbstractHandler as an interface as well, but you may want to implement some common logic here as well – shared across handlers.

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

Sidebar

Related Questions

First off I would like to say, that I am not trying to hack
First off, let me start off that I am not a .net developer. The
First off, I'm not really sure how much information is necessary to include because
First off, I’m not concerned with portability, and can safely assume that the endianness
First off let me say I am maintaining someone else's poorly designed code so
First off I'm really new to android (< 4 days). I'm trying to wrap
First off, apologies if this question has been asked before but I couldn't find
First off, I created a screen cast, in order to explain what I have
First off, I know my title can be formulated better, but my math classes
First off, I am not looking for a way to force the compiler to

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.