added glasspane makes calls to getMousePosition() for any component return null

157 Views Asked by At

I have written a custom glasspane component and set that to my JFrame and JDialog classes. I don't intercept mouse events in the custom glasspane, as I don't need to and also because there are a number of components on the GUI e.g. trees, popups etc which makes it complicated trying to intercept and forward mouse events. Everything works fine as is - except that whenever I call getMousePosition() on any of the other components (e.g. JPanel) in the frame/dialog it returns null.

On my JFrame and JDialog classes, I have set the glasspane to disabled (but visible) and it works for the most part. What do I need to do so the getMousePosition() method returns the correct value without having to add mouse event listeners to the glasspane component.

Sample code that demonstrates the problem.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class GlassPaneExample
{
private JPanel panel;
private JButton btn;
private JLabel response;
private int counter = 0;

GlassPaneExample()
{
    buildUI();
}

private void buildUI()
{
    JFrame frame = new JFrame("GlassPane Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(getPanel());
    frame.setGlassPane(new MyGlassPane());
    frame.getGlassPane().setVisible(true);
    frame.getGlassPane().setEnabled(false);
    frame.setPreferredSize(new Dimension(200, 220));
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

JPanel getPanel()
{
    panel = new JPanel(new BorderLayout());
    panel.addMouseMotionListener(new MouseMotionListener() {
        @Override
        public void mouseDragged(MouseEvent e)
        {
        }

        @Override
        public void mouseMoved(MouseEvent e)
        {
            System.out.println("mousePosition : " + panel.getMousePosition());
            System.out.println("mousePosition(true) : " + panel.getMousePosition(true));
        }
    });

    btn = new JButton("Click here...");
    btn.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            response.setText("Button click number " + getButtonClickCount());
        }
    });

    response = new JLabel();
    response.setToolTipText("Response label");

    panel.add(btn, BorderLayout.NORTH);
    panel.add(response, BorderLayout.SOUTH);
    return panel;
}

private int getButtonClickCount()
{
    return counter++;
}

public static void main(String[] arg)
{
    new GlassPaneExample();
}

class MyGlassPane extends JComponent
{
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        g.setColor(Color.GREEN);
        g.fillRect(10,40,120,120);
    }
}
}

If you comment out the 3 lines to do with adding the glasspane to the Frame, then the mouse position point is printed out correctly. This is a simple example to illustrate the problem, and I've said above, I dont want to add mouse listeners to the custom glasspane component as it introduces other complications.

0

There are 0 best solutions below