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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:59:28+00:00 2026-05-31T17:59:28+00:00

What is the solution to this problem? I added a mouse listener to a

  • 0

What is the solution to this problem?

I added a mouse listener to a JPanel, but that panel is full of buttons which cover the entire area of the JPanel.

Example (pseudo) code:

Create JPanel
Set to GridLayout
Add 25 buttons (5x5 grid)
Add MouseListener to JPanel

MouseListener/MouseMotionListener:

onMouseMove { print out X,Y co-ords of mouse }

The co-ords are never printed out until I get right to the edge of the JPanel container, because the buttons are blocking the rest of it.

How can I make the mouse listener work over all of the panel’s components without having to add the listener to each component – Or am I supposed to add the listener to each component?

ADDED INFO:
I’m trying to add a touch-gesture for a touch-screen system (swiping the panel causes an action to occur).
I’m pretty confident about recognising the gesture myself, but I was really looking for a ‘better’ way than to add a copy of the listener to each component (this would be even worse for me, because the components are changing).

I am going to try to add it to the glassPane instead… (at the moment getRootPane() gives me NullPointerException)

EDIT:
I now know that I’m trying to getRootPane() from a JPanel which is not the root container, thats why i’m getting a null. I need to do this on the main JFrame.

EDIT2:
Okay so I’ve done that (added glass pane to main JFrame), at first I had a problem because I didn’t do this:

myGlassPane.setVisible(true);

So it seemed like it wasn’t working. But once I did that all I had to do was set the opaque flag to false to make it transparent

myGlassPane.setOpaque(false);

So now i’m getting the X,Y co-ords printed out over the buttons and everything, however the buttons aren’t working because there’s a panel over them.

  • 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-31T17:59:30+00:00Added an answer on May 31, 2026 at 5:59 pm

    Replace JButton with a custom button. Forward the events you need from the button to the button’s parent. Here is an example that will forward the mouse entered event. Adjust forwarding and add exception/error handling as necessary.

    class JJButton extends JButton {
        {
            addMouseListener(new MouseAdapter(){
            public void mouseEntered(MouseEvent e) {
                if (JJButton.this.getParent()!=null){
                    MouseListener[] ml = JJButton.this.getParent().getMouseListeners();
                    for (MouseListener l : ml) {
                        l.mouseEntered(e);
                    }
                }
            }
            });
        }
        public JJButton(String string) {
            super(string);
        }    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've searched google and SO for a solution to this problem but have not
Can someone explain the solution of this problem to me? Suppose that you are
I realized the solution to this problem while I was creating the documentation to
Hope to get solution to this problem. I have been stuck on it since
I cannot find the solution for this problem, here is the simplified example: On
I was thinking of a solution to this problem. My input: 1. Have a
I have been struggling to find a solution to this problem. In my code,
I'm looking for a nice tight regex solution to this problem. I'm looking to
I haven’t been able to find a good solution for this problem on the
I've spent a good amount of time coming up with solution to this problem,

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.