Question

I have a JLayeredPane that has two layers. The top level is fully transparent and acts as a glass pane that captures mouse events and then dispatches them to the pane beneath it. The bottom layer does not draw correctly for an unknown reason. Do I have to dispatch painting events to it or something else?

By the way, I would use the Frame's glass pane, but I cannot get at it. I am stuck using a JPanel that is provided by the GUI framework built on top of Swing. I tried using SwingUtilities to get at the parent frame, but it always returns null.

Was it helpful?

Solution

If you want to use a glasspane and capture Events in Swing use the JLayer (Java 7). If you do not use Java 7 you can use the JXLayer (http://java.net/projects/jxlayer/). This is the new swing default component for this behavior. See

OTHER TIPS

I agree with Guillaune that its a very vague question. So you might well have a very good reason for doing it the way you have. However, if it helps you can probably dispose with the transparent layer altogether and simply attach your mouse listener directly to the JLayeredPane:

JPanel jP = new JPanel();
JLayeredPane jLP = new JLayeredPane();
MouseAdapter mouse = new MouseAdapter();
jLP.addMouseListener(mouse);

Then a MouseAdapter class:

public class MouseAdapter{
  //Make sure you override the methods
}

The other way, and one I use quite regularly, is to simply extend JLayeredPane and implement MouseListener:

public class MyLayers extends JLayeredPane implements MouseListener
{
  public MyLayers(){
    addMouseListener(this);
  }
//again make sure you override the methods
}

This throws a "leaking this in constructor" in NetBeans but its ignorable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top