Redispatching MouseEvents Through GlassPane

Hey all,
I'm trying to do some drawing on a GlassPane overtop of a JFrame consisting of several Components. I followed the tutorial on GlassPanes I found [here |http://java.sun.com/docs/books/tutorial/uiswing/components/rootpane.html#glasspane] to redispatch MouseEvents through the GlassPane to the proper Components underneath.
For the most part, this works perfectly. But there are several Components for which this doesn't seem to redispatch correctly (I used a JComboBox in the SSCCE, but I'm experiencing a similar problem with JTabbedPane as well). The problem arises when a Component contains "extra parts" that overlap another Component (like when the JComboBox's list extends on top of the JPanel.. Or, I'm assuming, when a JTabbedPane's tabs do the same).
Anyway, any insight into this would be much appreciated.
SSCCE:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class GlassPaneTest{
     JFrame frame = new JFrame("Testing GlassPane");
     public GlassPaneTest(){     
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          BoxLayout layout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
          frame.getContentPane().setLayout(layout);
          JPanel buttonPanel = new JPanel();
          buttonPanel.add(new JButton("button"));
          buttonPanel.add(new JComboBox(new String[]{"one", "two", "three"}));
          frame.add(buttonPanel);
          createGlassPane();
          frame.setMinimumSize(new Dimension(200,200));
          frame.setMaximumSize(new Dimension(200,200));
          frame.setPreferredSize(new Dimension(200,200));
          frame.setVisible(true);
     public void createGlassPane(){
          final Point mousePoint = new Point(0,0);
          final JPanel glassPanel = new JPanel(){
               public void paintComponent(Graphics g){
                    super.paintComponent(g);
                    g.setColor(Color.GREEN);
                    g.fillOval((int)mousePoint.getX(), (int)mousePoint.getY(), 20, 20);
          MouseAdapter mouseAdapter = new MouseAdapter(){
               public void mouseClicked(MouseEvent me){
                    redispatchMouseEvent(me);
               public void mouseEntered(MouseEvent me){
                    redispatchMouseEvent(me);
               public void mouseExited(MouseEvent me){
                    redispatchMouseEvent(me);
               public void mousePressed(MouseEvent me){
                    redispatchMouseEvent(me);
               public void mouseReleased(MouseEvent me){
                    redispatchMouseEvent(me);
               public void mouseDragged(MouseEvent me){
                    redispatchMouseEvent(me);
               public void mouseMoved(MouseEvent me){
                    mousePoint.setLocation(me.getX(), me.getY());
                    glassPanel.repaint();
                    redispatchMouseEvent(me);
               public void mouseWheelMoved(MouseWheelEvent me){
                    redispatchMouseEvent(me);
               public void redispatchMouseEvent(MouseEvent me){
                    Point glassPanelPoint = me.getPoint();
                    Container container = frame.getContentPane();
                    Point containerPoint = SwingUtilities.convertPoint(glassPanel,
                                                  glassPanelPoint, container);
                    if(containerPoint.getY() >= 0){
                         Component component =
                              SwingUtilities.getDeepestComponentAt(container, (int)containerPoint.getX(), (int)containerPoint.getY());
                         if(component != null){
                              Point componentPoint = SwingUtilities.convertPoint(glassPanel, glassPanelPoint, component);
                              System.out.println("mouse " + component.getClass().toString());
                              component.dispatchEvent(new MouseEvent(component,
                                        me.getID(),
                                        me.getWhen(), me.getModifiers(),
                                        (int)componentPoint.getX(),
                                        (int)componentPoint.getY(),
                                        me.getClickCount(),
                                        me.isPopupTrigger()));
                         else{
                              System.out.println("null component");
          glassPanel.addMouseListener(mouseAdapter);
          glassPanel.addMouseMotionListener(mouseAdapter);
          glassPanel.setOpaque(false);
          frame.setGlassPane(glassPanel);
          glassPanel.setVisible(true);
     public static void main(String [] argS){
          new GlassPaneTest();
}

jboeing wrote:
It appears that the Motif LnF doesn't actually contain the mouse-over highlight at all, even without a glass pane.:o right you are! I had to laugh at myself when I read this. I guess I was looking for things that went wrong since adding the GlassPane and started picking up on things that didn't actually change. Wow. Who knows how long I would have pounded away at that "problem" if you hadn't pointed that out?
Otherwise, I did successfully get the lightweight solution to work:Thanks so much for taking the time to come up with and offer a solution. I also came across [this page|http://weblogs.java.net/blog/alexfromsun/archive/2006/09/a_wellbehaved_g.html] offering another solution (even to some problems that I didn't know about at first, interesting read).
Following that page, I came up with this approach:
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class GlassPaneTest{
     JFrame frame = new JFrame("Testing GlassPane");
     JComboBox comboBox = new JComboBox(new String[]{"one", "two", "three"});
     public GlassPaneTest(){
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          BoxLayout layout = new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS);
          frame.getContentPane().setLayout(layout);
          JPanel buttonPanel = new JPanel();
          buttonPanel.add(new JButton("button"));
          buttonPanel.add(comboBox);
          frame.add(buttonPanel);
          createGlassPane();
          frame.setMinimumSize(new Dimension(200,200));
          frame.setMaximumSize(new Dimension(200,200));
          frame.setPreferredSize(new Dimension(200,200));
          frame.setVisible(true);
     public void createGlassPane(){
          final Point mousePoint = new Point(0,0);
          final JPanel glassPanel = new JPanel(){
               public void paintComponent(Graphics g){
                    super.paintComponent(g);
                    g.setColor(Color.GREEN);
                    g.fillOval((int)mousePoint.getX(), (int)mousePoint.getY(), 20, 20);
               public boolean contains(int x, int y){
                    return false;
          AWTEventListener awtListener = new AWTEventListener(){
               public void eventDispatched(AWTEvent e){
                    if(e instanceof MouseEvent){
                         MouseEvent me = (MouseEvent)e;
                         MouseEvent converted = SwingUtilities.convertMouseEvent(me.getComponent(), me, glassPanel);
                         mousePoint.setLocation(converted.getX(), converted.getY());
                         glassPanel.repaint();
          Toolkit.getDefaultToolkit().addAWTEventListener(awtListener, AWTEvent.MOUSE_MOTION_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK);
          glassPanel.setOpaque(false);
          frame.setGlassPane(glassPanel);
          glassPanel.setVisible(true);
     public static void main(String [] argS){
                    try {
                         UIManager
                         .setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
                    } catch (Exception e) {
                         e.printStackTrace();
          new GlassPaneTest();
}Does anybody see anything glaringly wrong with this? It seems to work perfectly, so I'm calling it a week :)
Thanks again!

Similar Messages

  • Redispatch MouseEvents?

    I have a custom list that listens for mouseDown and mouseUp.  It always receives mouseDown events, but mouseUp events are swallowed if they occur over an itemRenderer.  Is there a way to make the itemRenderer redispatch those events?  I know I could dispatch a custom event, but since the MouseEvent event has all the properties I care about, it would be nice to simply pass it on.

    D'oh, you got me.  I had tried to make my own event listener and had a name collision with List's own mouseUpHandler, so I just overrode it and called it with super.  It looks like the listener for that function is toggled on and off depending on various conditions, hence the lack of feedback.  Added my own event listener ( addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler2,false,0,true); ) and it works fine now.
    Thanks!

  • Resizing glass pane

    Disabling GUI controlls causing the loose of the controls beauty.
    However, sometimes it is neccessary, to display the control in
    a uneditable mode, especially when the input can not be consumed.
    The perfect idea to maintain the beauty of the GUI contorls and
    make them uneditable, is to put a glass infront of them, so you just
    look from pattern :).
    This is a greate idea provided through GlassPane in the Swing API.
    But, still there is a limitation, why?
    The glass pane should be as large as the JRootPane, which means
    that you have to cover the whole top-level container :(.
    Take this example:
    I have a TabbedPane that consists of multiple tabs and the tabbed pane is loacted in a JApplet. I need to move between the tabs while making the controls in side the tabbs uneditable. Here I was constrained by the
    Glass Pane since it covers the whole applet's area, and no more accessability to the tabbs.
    Some one said: redispatch the event to the TabbedPane, I said: Ok, but
    no method in the TabbedPane tells me which tab has the x,y point.
    I suggest to make the GlassPane resizable, and to provide a method in
    the TabbedPane which takes the x,y a returns the index of the selected
    tab.
    Please what do you think ? :-)

    I think it is not useful because you can create your own glasspane for any component you want !
    Look at this thread :
    http://forum.java.sun.com/thread.jsp?forum=57&thread=268566
    I hope this helps,
    Denis

  • How to set picture instead of any color to Frame

    hello friends.i justwanna to know that how can we set a picture as a background of any Frame.is there any way through glasspane.

    You should override the paintComponent() method, not the paint() method. Here is a link to the Swing tutorial on "Painting" which will explain why:
    http://java.sun.com/docs/books/tutorial/uiswing/overview/draw.html
    Don't forget to click on the "Working With Graphics" link on the bottom.
    Here is a little program to get you started:
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    public class TestBackground extends JFrame
         ImageIcon img;
         public TestBackground()
              img = new ImageIcon("????.jpg");
              JPanel panel = new JPanel()
                   public void paintComponent(Graphics g)
                        g.drawImage(img.getImage(), 0, 0, null);
                        super.paintComponent(g);
              panel.setOpaque(false);
              panel.add( new JButton( "Hello" ) );
              setContentPane( panel );
         public static void main(String [] args)
              TestBackground frame = new TestBackground();
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setSize(300, 300);
              frame.setVisible(true);

  • GlassPane and event handling

    Hi all,
    Is it possible that I could use the glass pane of my applet to catch the user right click ONLY, and forward (or redispatch) all other events to whoever else is listening.
    I have a generic right click menu for the entire applet and it seems silly having to drill down through the component hierachy to have the right click available anywhere in the applet..., so far I've only managed to block all other events and obviously respond to right click popups.
    Ta for any help.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class bradmallan {
      public static void main(String[] args) {
        final JPanel panel = new JPanel(new GridBagLayout());
        panel.setBackground(Color.green);
        panel.setPreferredSize(new Dimension(300,200));
        final JButton leftButton = new JButton("Left");
        final JButton rightButton = new JButton("Right");
        final JButton lowButton = new JButton("Low");
        ActionListener l = new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            JButton button = (JButton)e.getSource();
            System.out.println(e.toString().substring(
                               e.toString().indexOf("text") + 5,
                               e.toString().lastIndexOf(",")));
        leftButton.addActionListener(l);
        rightButton.addActionListener(l);
        lowButton.addActionListener(l);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.gridwidth = gbc.RELATIVE;
        panel.add(leftButton, gbc);
        gbc.gridwidth = gbc.REMAINDER;
        panel.add(rightButton, gbc);
        panel.add(lowButton, gbc);
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(panel);
        Component glassPane = f.getGlassPane();
        glassPane.setVisible(true);
        glassPane.addMouseListener(new MouseAdapter() {
          public void mousePressed(MouseEvent e) {
            if(e.getButton() == MouseEvent.BUTTON1) {
              int x = e.getX();
              int y = e.getY();
              Component component = SwingUtilities.getDeepestComponentAt(panel,x,y);
              if(component.equals(leftButton))
                leftButton.doClick();
              if(component.equals(rightButton))
                rightButton.doClick();
              if(component.equals(lowButton))
                lowButton.doClick();
              System.out.println("x = " + x + "\ty = " + y);
            if(e.getButton() == MouseEvent.BUTTON3)
              System.out.println("Button 3");
        f.pack();
        f.setLocation(400,300);
        f.setVisible(true);
    }

  • What is on top: GlassPane or Popup?

    The Javadoc say: The glassPane sits on top of all other components in the JRootPane.
    In j2sdk 1.4:
    I have set a glasspane on a JFrame and set the glasspane to visible. I am letting the glasspane handle all mouse and key events.
    If I then redispatch a popup event to a component behind the glasspane and bring up the JPopupMenu, it is displayed in front of the glasspane (glasspane no longer recieves mouseevents).
    This is only a problem in the 1.4 version. In 1.3.1 the JPopupMenu is displayed behind the glasspane.
    Has anyone had the same problem and is there a way to solve it?

    I think it is because, it use heavyweight popup which are in heavyweighht Window, so popup is in front of the glasspane.
    Try :
    JPopupMenu.setDefaultLightWeightPopupEnabled(true);
    I hope this helps,
    Denis

  • GlassPane - JComboBox

    Hi,
    I have a glass pane, which redispatches all mouse, mouse motion, and mouse wheel events. I have a JComboBox under the glass pane. When I mouse over the combo and click on it, the popup menu appears. But when I move my mouse on the popup menu, the item that my mouse moves over does not become selected/highlighted as normal. I can select the item, but it doesnt look like I am selecting it.
    Any ideas?
    I already set, JComboBox.setLightWeightPopupEnabled(false);

    My 2 cents about searching for deep components...
    I usually start searches for components at the window level.
    You suggest doing it at the layered pane instead of the content pane (since the content pane is below the layered pane, searching the content pane misses components in the layered pane). Good idea, but why stop there?
    Take it a step further... the JRootPane is next up, which is just a JComponent containing the glasspane, layered pane, content pane, etc, and the Window is above that. Just start searching at the window, which you know is at the top of the component hierarchy.
    Technically it's possible to put components directly in the Window, so you can't really say you've found the deepest component unless you search starting at the window.
    Also, SwingUtilities only returns the deepest VISIBLE component (i.e., the component must be realized in the component hierarchy) If you are searching for something that renders itself and is not in the component hierarchy, then you will not find it.

  • Semi-transparent GlassPane

    How to make a semi-transparent glasspane to cover entire JFrame so all components in contentPane can be saw through?

    That's quite easy:
        private class HelpGlassPane extends JComponent {
            public HelpGlassPane() {
            public void paint(Graphics g) {
                Graphics2D g2D = (Graphics2D)g;
                g2D.setColor(Color.black);
                g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER , 0.2f));
                g2D.fillRect(0, 0, getWidth(), getHeight());
       }Could be that you can have the same effect even with less code, but I'm doing a lot more in my glass pane, so this is only part of the code.
    Also it might be better to override paintComponent() instead of paint()
    Note that cou can even let all mouse events 'fall through' if you override this method to always return false in your glass pane:
    boolean JComponent.contains(int x, int y)
    Hope it helps,
    Stephen

  • GlassPane problem

    Hi all,
    I've tried to put a glasspane over some JButtons/JTable/JComboBox in order to better control the focus & field validation. Here is the Sun official tutorial I am looking at:
    http://java.sun.com/docs/books/tutorial/uiswing/components/rootpane.html
    Here they emphasis to redispatch mouseDrag event:
    "... If a mouse event occurs over the check box or menu bar, then the glass pane redispatches the event so that the check box or menu component receives it. So that the check box and menu behave properly, they also receive all drag events that started with a press in the button or menu bar ..."
    Surprise me much, they know the problem but their sample code does not work. When the glasspane is on (visiable), I try to click on the check box, keep mouse pressed and drag off the check box, the check box still stays grayed. Similar thing happens on Jbutton, scroll bar.
    Anyone has any idea on this ? Your help will be greatly appreicated.
    KC

    If you would explain what you are trying to do, then maybe we could actually help you. Post your question in the form of a requirement.
    You may be using the wrong approach to solve the problem, but if we don't know what the problem is then we can't offer alternatives.
    As a wild guess maybe crwoods DimOverlay example in this posting will help:
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=791415

  • Glasspane  with alpha and resize troubles

    I use the following custom JPanel as glasspane on a JFrame, note the alpha value on color. When resizing the frame the content underneath the glasspane is not repainted, why?
    private class PauseGlassPane extends JPanel {
    Color color = new Color(255,255,255,128);
    Rectangle clipBounds;
    public PauseGlassPane() {
    addKeyListener(new java.awt.event.KeyAdapter() {
    // grab keyevents
    addMouseListener(new java.awt.event.MouseAdapter() {
    // grab mouseevents
    setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
    setOpaque(true);
    public void paint(Graphics g) {
    clipBounds = g.getClipBounds();
    g.setColor(color);
    g.fillRect(clipBounds.x, clipBounds.y, clipBounds.width, clipBounds.height);
    g.dispose();
    }

    Hi Flex harUI, I read your replay,
    but add SWFLoader.ValidateNow(); doesn't seem to solve it. 
    I think I need to wait for updateComplete event, but I don't know how to write this. 
    Thank you for your attention,
    Michele

  • GlassPane: How to fire the ActionEvent of a JPanel

    Hello Everyone,
    I'm developing an application with a glasspane added to it. The idea is that some JPanels which act as an Button
    can be "clicked" through the glasspane.The other components may not be clicked when the glasspane is enabled
    When i click on the glasspane on a location where a JPanel is located, the correct Jpanel is found by getDeepestComponentAt (tested by using System.out).
    but when i dispatch the event(mouseclick) on this JPanel the actionEvent isn't fired.
    The JPanels are located in another class file then the glasspane is.
    My question is how to fire a ActionEvent from another class?
    Thanks in advance,

    If you post a SSCCE that demonstrates your problem, someone might be able to help.
    {color:#0000ff}http://www.physci.org/codes/sscce.html{color}
    db

  • GlassPane functionality in AWT

    Hi all,
    I have a program I am trying to write that uses a Panel from an applet. The panel has a mouseListener that I want to block, so I figured I would use the JFrame's GlassPane to intercept the mouse events, which works fine if the underlying panel is a JPanel, but not if it is an awt.Panel, which is what I need (probably a difference between heavyweight vs. lightweight components).
    So how can I go about intercepting the mouse events for an AWT Panel if it will always be on top of any of my Swing components? Are there any AWT components that I can make invisible and place on top of the Panel?
    Thanks for any help...
    (oh, I was thinking of removing or qualifying the mouse events inside the Panel's mouse listener, but the Panel needs to be serialized and then re-displayed in the Applet for which it was originally designed, so I would rather not do that...)

    Anybody any help with this?
    I have been experimenting with Window, but it doesn't take any of the Events either:
         JFrame frame = new JFrame("Glass Pane Test");
         JLabel jl = new JLabel ("Swing");
         JPanel jp = new JPanel();
         jp.add(jl);
         jp.addMouseListener(new MouseAdapter() {
           public void mouseClicked(MouseEvent e) {
              System.out.println("SWING: Not the Glass Pane Event");
         Label l2 = new Label("AWT");
         Panel p = new Panel();
         p.add(l2);
         p.addMouseListener(new MouseAdapter() {
           public void mouseClicked(MouseEvent e) {
              System.out.println("AWT: Not the Glass Pane Event");
         frame.getGlassPane().addMouseListener(new MouseAdapter() {
           public void mouseClicked(MouseEvent e){
              System.out.println("SWING: I AM The Glass Pane Event");
         JWindow w = new JWindow ();
         w.addMouseListener(new MouseAdapter() {
           public void mouseClicked(MouseEvent e){
              System.out.println("AWT: I AM The Glass Pane Event");
         w.setBounds(frame.getBounds());
    /*1*/     frame.getContentPane().add(jp);
         frame.pack();
         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
         frame.setVisible(true);
    /*2*/     //frame.getGlassPane().setVisible(true);
    /*3*/     w.show();
    /*3*/     w.toFront();
         System.out.println(w.isVisible());If I comment out /*3*/ and uncomment /*2*/ Then we can see the behaviour of Panel with the glass pane (by switching /*1*/ between add(jp) and add(p). Then we reinstate /*3*/ and comment out /*2*/ and all the comments go to the Panel/JPanel, none go to the Window... Id there a way to make the Window get all the Mouseevents?

  • Intercepting mouseEvents over a panel

    Hi,
    I want to be able to intercept all mouse events over one panel in my frame so the events are not passed onto the components on that panel, while at the same time have the other components in the frame receive events as normal, so basically i want to know if i can set the glasspane visible only over a certain component.
    Thanks
    Michael

    Check out the tutorial.
    Trail: Creating a GUI with JFC/Swing
    Lesson: Using Swing Components
    The glass pane example will help you.It shows how to programaticaly redispatch the mouse events.

  • GlassPane and JMenuBar -- PLEASE HELP

    I am implementing my own glasspane. I am having trouble with JMenuBar. I read javadocs and notes and I still can't get it to work. First thing that I do is that for each mouseevent i call redispatchMouseEvent();
    public void mouseMoved(MouseEvent me) {     redispatchMouseEvent(me);  }
    public void mouseDragged(MouseEvent me) {redispatchMouseEvent(me); }
    public void mouseClicked(MouseEvent me) {redispatchMouseEvent(me);}
    public void mouseEntered(MouseEvent me){redispatchMouseEvent(me);}
    public void mouseExited(MouseEvent me){redispatchMouseEvent(me); }
    public void mousePressed(MouseEvent me){redispatchMouseEvent(me);}
    public void mouseReleased(MouseEvent me){redispatchMouseEvent(me);    }Inside redispatchMouseEvent() I check to see if mouse is on jMenuBar, if so I get instance of JMenu selected and save it;
         if (containerPoint.y < 0)
             if (containerPoint.y + menuBar.getHeight() >= 0)
    // MOUSE IS ON JMENUBAR AREA
              containerPoint = SwingUtilities.convertPoint(this, glassPanePoint, menuBar);
              component = SwingUtilities.getDeepestComponentAt(menuBar, containerPoint.x, containerPoint.y);
              Point componentPoint = SwingUtilities.convertPoint(this, glassPanePoint, component);
              if (component != null)
                  component.dispatchEvent(new MouseEvent(component, me.getID(), me.getWhen(), me.getModifiers(), componentPoint.x, componentPoint.y, me.getClickCount(), me.isPopupTrigger()));
    // SAVE HANDLE TO JMENU
                  if (component instanceof JMenu)
                   menu = (JMenu)component;
           }I check to see if (menu != null ) if not then I'm inside a menu. so I call processMouseEvent to handle movement and selection of a JMenuItem, the problem that I am getting is that as the mouse moves the JMenuItems are highlighted properly but when I click on it nothing happens. what am I doing wrong?
    else
             if (menu != null)
              System.out.println("menu event");
              Point componentPoint = SwingUtilities.convertPoint(this, glassPanePoint, menu);
              MenuSelectionManager.defaultManager().processMouseEvent((new MouseEvent(menu, me.getID(), me.getWhen(), me.getModifiers(), componentPoint.x, componentPoint.y, me.getClickCount(), me.isPopupTrigger())));
             }elsePLEASE HELP!!!
    Here is complete source excerpt;
        // mouse listeners
        public void mouseMoved(MouseEvent me)
         redispatchMouseEvent(me);
        public void mouseDragged(MouseEvent me)
         redispatchMouseEvent(me);
        public void mouseClicked(MouseEvent me)
         System.out.println("click");
         menu = null;
         redispatchMouseEvent(me);
        public void mouseEntered(MouseEvent me)
         redispatchMouseEvent(me);
        public void mouseExited(MouseEvent me)
         redispatchMouseEvent(me);
        public void mousePressed(MouseEvent me)
         System.out.println("press");
         redispatchMouseEvent(me);
        public void mouseReleased(MouseEvent me)
         System.out.println("release");
         redispatchMouseEvent(me);
        JMenu menu;
        private void redispatchMouseEvent(MouseEvent me)
         Point glassPanePoint = me.getPoint();
         Container container = contentPane;
         Point containerPoint = SwingUtilities.convertPoint(this, glassPanePoint, contentPane);
         Component component = SwingUtilities.getDeepestComponentAt(container, containerPoint.x, containerPoint.y);
         if (containerPoint.y < 0)
             if (containerPoint.y + menuBar.getHeight() >= 0)
              System.out.println("MenuBar:component="+component);
              containerPoint = SwingUtilities.convertPoint(this, glassPanePoint, menuBar);
              component = SwingUtilities.getDeepestComponentAt(menuBar, containerPoint.x, containerPoint.y);
              Point componentPoint = SwingUtilities.convertPoint(this, glassPanePoint, component);
              if (component != null)
                  component.dispatchEvent(new MouseEvent(component, me.getID(), me.getWhen(), me.getModifiers(), componentPoint.x, componentPoint.y, me.getClickCount(), me.isPopupTrigger()));
                  if (component instanceof JMenu)
                   menu = (JMenu)component;
             else
              System.out.println("Decor");
         else
             if (menu != null)
              System.out.println("menu event");
              Point componentPoint = SwingUtilities.convertPoint(this, glassPanePoint, menu);
              MenuSelectionManager.defaultManager().processMouseEvent((new MouseEvent(menu, me.getID(), me.getWhen(), me.getModifiers(), componentPoint.x, componentPoint.y, me.getClickCount(), me.isPopupTrigger())));
             }else
              if (component != null)
                  System.out.println("component");
                  Point componentPoint = SwingUtilities.convertPoint(this, glassPanePoint, component);
                  component.dispatchEvent(new MouseEvent(component, me.getID(), me.getWhen(), me.getModifiers(), componentPoint.x, componentPoint.y, me.getClickCount(), me.isPopupTrigger()));
         repaint();
    }

    You need to set popup menus to be non-lightweight in your GUI code, before you add your JMenu objects to the JMenuBar:
    JPopupMenu.setDefaultLightWeightPopupEnabled(false);I'm not entirely sure why this is necessary here, so if someone would care to explain, I'm all ears ;)
    Your code is overly complicated too. You don't need to test for instanceof JMenu or anything like that. Your redispatch method should look something like this (adapted from the Sun tutorial):
    private void redispatchMouseEvent(MouseEvent event) {
              Point glassPanePoint = event.getPoint();
              Container container = contentPane;
              // Get mouse event point in content pane's coordinate system
              Point containerPoint = SwingUtilities.convertPoint(glassPane, glassPanePoint, contentPane);
              // If we're not in the content pane
              if (containerPoint.y < 0) {
                   // Then we're either in the menu bar..
                   if (containerPoint.y + menuBar.getHeight() >= 0) {
                        // Get mouse event point in menu bar's coordinate
                        // system
                        Point menuBarPoint = SwingUtilities.convertPoint(glassPane, glassPanePoint, menuBar);
                        // Get deepest visible component lying under the mouse event
                        Component component = SwingUtilities.getDeepestComponentAt(menuBar, menuBarPoint.x, menuBarPoint.y);
                        // Do nothing if no component is found
                        if (component != null) {
                             // Get mouse event point in deepest component's coordinate
                             // system
                             Point componentPoint = SwingUtilities.convertPoint(glassPane, glassPanePoint, component);
                             // Redispatch mouse event to the deepest component
                             component.dispatchEvent(new MouseEvent(component, event.getID(), event.getWhen(), event.getModifiers(), componentPoint.x,
                                       componentPoint.y, event.getClickCount(), event.isPopupTrigger()));
                        // ..or else we're over a non-system window decoration
                   } else {
              } else {
                   // Get deepest visible component lying under the mouse event
                   Component component = SwingUtilities.getDeepestComponentAt(container, containerPoint.x, containerPoint.y);
                   // Do nothing if no component is found
                   if (component != null) {
                        // Get mouse event point in deepest component's coordinate
                        // system
                        Point componentPoint = SwingUtilities.convertPoint(glassPane, glassPanePoint, component);
                        // Redispatch mouse event to the deepest component
                        component.dispatchEvent(new MouseEvent(component, event.getID(), event.getWhen(), event.getModifiers(), componentPoint.x,
                                  componentPoint.y, event.getClickCount(), event.isPopupTrigger()));
         }HTH.

  • JPanel overlay which grabs all MouseEvents

    Hi all.
    I'm getting stuck with an OverlayLayout and a transparent JPanel.
    What I have is a JPanel with several components on it (Textfields, Comboboxes, ..)
    In the running application I need to be able to re-arrange the components using
    drag & drop but this feature can be disabled so the UI behaves like every other you know.
    (You can think of this as a minimalistic GUI designer).
    My approach was to place another (transparent) JPanel on top of it using OverlayLayout to
    be able to drag ghost copies of my components around with the mouse.
    This panel can be switched invisible so dragging is disabled.
    This works for components which do not react on mouse events for themselves
    (JLabels, ..) but not for textfields.
    What I would need is a transparent layer which is able to grab all MouseEvents so the
    components on the underlying panel will not receive them (textfields don't get the focus, buttons don't get clicked, ...).
    Any help really appreciated.
    Kind regards
    Carsten

    You can use a glass pane to intercept all the events. Just try to delete "Edit Me" from this text box!
    package mypackage;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class GlassPanelTest extends JFrame {
        public GlassPanelTest() {
            super("Glass Panel Test");
            setBounds(10, 10, 380, 200);
            Container contentPane = getContentPane();
            contentPane.add(new JTextField("Edit Me"));
            Component glassPane = getGlassPane();
            glassPane.setVisible(true);
            glassPane.addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent _e) {
                    System.out.println("caught by the glass pane");
        public static void main(String[] args) {
            GlassPanelTest app = new GlassPanelTest();
            app.setVisible(true);       
    }

Maybe you are looking for

  • What's the best way to store movies?

    Hello, I have burned almost all of my videos to the iMac, and subsequently to iTunes and Apple TV.  I'm now running out of disk space on the iMac, no surprise.  I have a cloud based backup system (not iCloud), but it's not fast enough to support actu

  • XML output File Format change

    Hi experts, I am currenty getting below output in my XML file and it is wrapped in message type MT_Test. <?xml version="1.0" encoding="UTF-8"?> <ns0:MT_Test xmlns:ns0="http://test.com/xi/T1/Data"> <Tests> <schemaLocation>http://tempuri.org/TestSchema

  • Sharing of Child TAB

    Hi All, I have an Parent tab and 4 child tabs associated to this parent tab. For Example 'A' is the parent tab and '1', '2', '3' and '4' are the child tabs of the parent 'A'. I have created one page in my application to manage admin users. I need to

  • Need to find BAPI_PROJECT_MAINTAIN in ES Service Registry

    I need to do some testing with BAPI_PROJECT_MAINTAIN. What's the equivalent Enterprise Services Workplace function WSDL? How do I find this for testing?

  • SPRY Accordion Linking

    I want to link certain text from a homepage to a second page that contains a SPRY Accordion with three panels. But I want to link to the second panel open, even though this is not the default open panel if you load that page independantly. Is there a