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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:42:46+00:00 2026-05-25T10:42:46+00:00

I have a class whitch extends JPanel: public class ButtonPanel extends JPanel { private

  • 0

I have a class whitch extends JPanel:

public class ButtonPanel extends JPanel {

    private label;

    public ButtonPanel() {
        label=new JLabel("waiting for click");
        add(label);
    }

    public void setButtonText() {
        label.setText("just clicked");
    }

}

I have several instances of that class which is added to JFrame. I want to create one instanse of MouseAdapter class and then add them as a mouse listener to all of the ButtonPanel components on my JFrame. I meen:

ButtonPanel butt1 = new ButtonPanel();
ButtonPanel butt2 = new ButtonPanel();
ButtonPanel butt3 = new ButtonPanel();
//... here goes code which add ButtonPanels to JFrame

MouseAdapterMod mam = new MouseAdapterMod();
butt1.addMouseListener(mam);
butt2.addMouseListener(mam);
butt3.addMouseListener(mam);

The MouseAdapterMod class I want to be separate from the other and locate in it’s own package. It should looks like this:

public class MouseAdapterMod extends MouseAdapter {

    public void mouseClicked(MouseEvent e) {
        //here goes the code of calling setButtonText method of ButtonPanel component on which the event had occurred
    }
}

So the problem is that I don’t know how to implement mouseClicked method to make it determine which of ButtonPanel generate the event and call the corresponding to that component setButtonText() method. Is anyone know how to do that?

I know that I can achieve this by including event handling functionality in the ButtonPanel class, but thats not appropriate way for me, cuz I want to keep the class structure as I described above and have only one instance of MouseAdapterMod class for handling all of the ButtonPanels.

  • 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-25T10:42:46+00:00Added an answer on May 25, 2026 at 10:42 am

    The MouseEvent#getSource method will return which object has been clicked:

    public class MouseAdapterMod extends MouseAdapter {
    
       // usually better off with mousePressed rather than clicked
       public void mousePressed(MouseEvent e) {
           ButtonPanel btnPanel = (ButtonPanel)e.getSource();
           btnPanel.setButtonText();
       }
    }
    

    As the comments note, you’re often better off listening for mousePressed or mouseReleased rather than mouseClicked because for mouseClicked to work, the press and release must be from the same point, and if the mouse shifts even a slight amount, the click won’t register.

    My test program:

    import java.awt.Color;
    import java.awt.GridLayout;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class MainForButtonPanel extends JPanel {
       public MainForButtonPanel() {
          setLayout(new GridLayout(4, 4));
    
          MouseAdapter myMA = new MouseAdapterMod();
    
          for (int i = 0; i < 4; i++) {
             for (int j = 0; j < 4; j++) {
                ButtonPanel btnPanel = new ButtonPanel();
                btnPanel.addMouseListener(myMA);
                add(btnPanel);
             }
          }
    
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("MainForButtonPanel");
          frame.getContentPane().add(new MainForButtonPanel());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    
    class ButtonPanel extends JPanel {
    
       private static final int TIMER_DELAY = 2000;
       private static final String JUST_CLICKED = "just clicked";
       private static final String WAITING_FOR_CLICK = "waiting for click";
       private static final Color CLICKED_COLOR = Color.pink;
       private JLabel label;
    
       public ButtonPanel() {
          label = new JLabel(WAITING_FOR_CLICK);
          add(label);
       }
    
       public void setButtonText() {
          label.setText(JUST_CLICKED);
          setBackground(CLICKED_COLOR);
    
          new Timer(TIMER_DELAY, new ActionListener() {
             public void actionPerformed(ActionEvent ae) {
                label.setText(WAITING_FOR_CLICK);
                setBackground(null);
                ((Timer)ae.getSource()).stop();
             }
          }).start();
       }
    
    }
    
    class MouseAdapterMod extends MouseAdapter {
    
       // usually better off with mousePressed rather than clicked
       public void mousePressed(MouseEvent e) {
           ButtonPanel btnPanel = (ButtonPanel)e.getSource();
           btnPanel.setButtonText();
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class which looks something like this: public class Test { private
I wrote this code: public class FileViewer extends JPanel implements ActionListener { /** *
I have a class which simply extends JPanel and overrides the paintComponent method as
I have created a class called SimpleCanvas which extends the JPanel class, and I
I have two Java classes: B, which extends another class A, as follows :
I have a class which is marked with a custom attribute, like this: public
I have a class which has the following constructor public DelayCompositeDesigner(DelayComposite CompositeObject) { InitializeComponent();
I have a class which inherits from QTreeWidgetItem and I intercept the click event.
I have a public class which has the following method and instance variable: public
I have the following code. Class KochSnowflakesMenu is a grid JPanel with three buttons.

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.