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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:28:17+00:00 2026-05-16T12:28:17+00:00

I have a main window: public class MainPanel extends JFrame implements MouseListener { public

  • 0

I have a main window:

public class MainPanel extends JFrame implements MouseListener {

   public MainPanel() {
      setLayout(new FlowLayout());
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      addMouseListener(this);

      ChildPanel child = new ChildPanel();
      add(child);

      JPanel spacer = new JPanel();
      spacer.setPreferredSize(new Dimension(50, 50));
      add(spacer);

      pack();
      setLocationRelativeTo(null);
   }

   @Override
   public void mouseClicked(MouseEvent e) {
      System.out.println("Mouse click event on MainPanel");
   }
}

And a child JPanel:

public class ChildPanel extends JPanel implements MouseListener {

   public ChildPanel() {
      setBackground(Color.RED);
      setPreferredSize(new Dimension(200, 200));
      //addMouseListener(this);
   }

   @Override
   public void mouseClicked(MouseEvent e) {
      System.out.println("Mouse click event on ChildPanel");
   }
}

With the call to addMouseListener commented out in the child panel, the parent receives click events when I click anywhere in the window, including on the child. If I uncomment that call and click on the child panel, only the child receives the click event and it doesn’t propagate to the parent.

How do I stop the event from being consumed by the child?

  • 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-16T12:28:17+00:00Added an answer on May 16, 2026 at 12:28 pm

    I don’t think you can. I believe it’s a Swing design principle that only one component receives an event.

    You can get the behavior you want, however, but pass the JFrame to the ChildPanel and calling its mouseClicked(MouseEvent) or whatever method you want. Or just get the parent component.

       @Override
       public void mouseClicked(MouseEvent e) {
          System.out.println("Mouse click event on ChildPanel");
          this.frame.mouseClicked(e);
          getParent().mouseClicked(e);
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a KMainWindow: //file.h class MainWindow: public KMainWindow { public: MainWindow(QWidget *parent =
I have a main window (#1) on my webpage from which I open a
I have an Air application with a main window. I would like to have
I have a file called hellowf.cs class MyFirstApp { static void Main() { System.Windows.Forms.MessageBox.Show(Hello,
I have this problem. I create two instaces of my classes in main window.
I have this snippet of the code: public class Main_class { public static void
I have a window that contains a label (player1). I also have a class
Say I have the following code: import java.lang.InterruptedException; import javax.swing.SwingWorker; public class Test {
I am new to java and I have two classes (class calc and button)
I have a small application to convert several file formats, with a main windows

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.