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

The Archive Base Latest Questions

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

I had a different problem first, so I made this post: Java JPanel mouse

  • 0

I had a different problem first, so I made this post:
Java JPanel mouse listener doesn't work over its components

The answers led me to arrive at a java GlassPane. I saw the other posts on SO about this and they all point to this article:
http://weblogs.java.net/blog/alexfromsun/archive/2006/09/a_wellbehaved_g.html.

I used the example from the article (FinalGlassPane.java) and set it up exactly as in the test app:

GlassPane glass = new GlassPane(this);
getRootPane().setGlassPane(glass);
GestureListener gl = new GestureListener();
glass.addMouseMotionListener(gl);
glass.addMouseListener(gl);
glass.setVisible(true);
if (glass instanceof AWTEventListener) {
    AWTEventListener al = (AWTEventListener) glass;
    Toolkit.getDefaultToolkit().addAWTEventListener(al,
            AWTEvent.MOUSE_MOTION_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK);
}

But I still have the same problem, the glass prevents me from being able to click any buttons (JButton) below it.

If you’ve been kind enough to read my initial SO question and think glassPane is not for me, please post me an alternative solution, otherwise please tell me why this isn’t working.

Thanks in advance.

EDIT: IT WORKS but stops working when I add my own MouseListener

Notice in my code I was adding my own GestureListener, I can’t add my own MouseListener to this implementation – that’s what makes it not work. I took those lines off and the glass pane is now working correctly.

There is this notice in the code (FinalGlassPane.java) regarding MouseListener which I don’t really understand, so my new question is how can I add my own MouseListener to this glass pane?

/**
 * If someone adds a mouseListener to the GlassPane or set a new cursor
 * we expect that he knows what he is doing
 * and return the super.contains(x, y)
 * otherwise we return false to respect the cursors
 * for the underneath components
 */
@Override
public boolean contains(int x, int y) {
    if (getMouseListeners().length == 0 && getMouseMotionListeners().length == 0
            && getMouseWheelListeners().length == 0
            && getCursor() == Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)) {
        return false;
    }
    return super.contains(x, y);
}

ADDITIONAL INFO:

If I always return false in the contains(int, int) method the buttons always work, but my MouseListener on the GlassPane doesn’t. If I always return “super.contains(x,y)” then the opposite happens: MouseListener on the GlassPane works but I can’t click any components underneath.

FINAL EDIT
So I came to the conclusion that I can’t (or don’t) have a way (with Java 6) to allow my Application to have 2 layers which can receive MouseListener events (mousePressed, mouseReleased) at the same time. In the end I used the solution on my original question, which was to create a custom JButton and have that add the MouseListener to each instance.

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

    not clear from your both questions, if you you really needed convertXxx or getXxx methods from SwingUtilities

    1) you can put JLabel (dispatch mouse events) to the GlassPane, this JLabel could be placed / covering whole available Rectangle from JFrame, or only required Bounds

    2) you can put JLabel (dispatch mouse events) to the JViewport, this JLabel could be placed / covering whole available Rectangle from JScrollPane / JViewport, or only required Bounds

    3) you can use JLayeredPane (max 6 layers)

    4) you can use JLayer (Java7) based on JXLayer (for Java6 and maybe better choice)

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

Sidebar

Related Questions

I recently had to work around the different default sizes used for enumerations in
I've never had this issue before, so I'm somewhat lost. I'm getting two different
EDIT: So apparently I was doing everything right, but I had a different problem
Before describing the problem, let me first point out that this is a distinct
I'm sure I'm making a rookie mistake with java(this is actually my first program).
First: This is not a repeat of this question . Same script, but different
Has anyone had any success running two different web servers -- such as Apache
i've populated a dropdownlist control with different text properties but each text properties had
Had a coworker ask me this, and in my brain befuddled state I didn't
Had a page that was working fine. Only change I made was to add

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.