Re: Mouse Events not being registered

From: John Champaign (jchampai_at_hopper.math.uwaterloo.ca)
Date: 02/25/04


Date: Tue, 24 Feb 2004 19:05:02 -0500

Hi Tom,

That does seem to be exactly what I want, and seems to be a fairly clean
way of doing it. Thanks so much!

John

On Tue, 24 Feb 2004, Tom N wrote:

> "Tom N" wrote:
> > "The Glass Pane
> > If you make the glass pane visible, then it's like a *** of glass over
> all
> > the other parts of the root pane. It's completely transparent unless you
> > implement the glass pane's paintComponent method so that it does
> something,
> > and it intercepts input events for the root pane.
> >
> > The glass pane is useful when you want to be able to catch events or paint
> > over an area that already contains one or more components. For example,
> you
> > can deactivate mouse events for a multi-component region by having the
> glass
> > pane intercept the events. Or you can display an image over multiple
> > components using the glass pane."
> >
> > http://java.sun.com/docs/books/tutorial/uiswing/components/rootpane.html
>
> Here's some code based on the above. When the user presses the mouse down,
> the event is discarded. When they release the mouse button over a JButton,
> the JButton gets a mouse pressed then a mouse released, and the JButton's
> action is invoked.
>
> In the JFrame...
>
> getGlassPane().addMouseListener(new MA("Glasspane", contentPane));
> getGlassPane().setVisible(true);
> button.setAction(new AbstractAction()
> {
> public void actionPerformed(ActionEvent e)
> {
> System.out.println("Button pressed");
> System.out.flush();
> }
> }
> );
>
> class MA extends MouseAdapter
> {
> private String name;
> private Container container;
> MA(String name, Container container)
> {
> this.name = name;
> this.container = container;
> }
> public void mouseClicked(MouseEvent e)
> {
> redispatchMouseEvent(e, " mouseClicked ");
> }
> public void mouseEntered(MouseEvent e)
> {
> redispatchMouseEvent(e, " mouseEntered ");
> }
> public void mouseExited(MouseEvent e)
> {
> redispatchMouseEvent(e, " mouseExited ");
> }
> public void mousePressed(MouseEvent e)
> {
> redispatchMouseEvent(e, " mousePressed ");
> }
> public void mouseReleased(MouseEvent e)
> {
> redispatchMouseEvent(e, " mouseReleased ");
> }
> private void redispatchMouseEvent(MouseEvent e, String type)
> {
> System.out.println(name + type + e.getWhen());
> System.out.flush();
> if (container == null)
> return;
>
> if (e.getID() == e.MOUSE_PRESSED)
> {
> System.out.println("Ignored");
> System.out.flush();
> return;
> }
>
> Point glassPanePoint = e.getPoint();
> Container container = contentPane;
> Point containerPoint = SwingUtilities.convertPoint(
> getGlassPane(),
> glassPanePoint,
> contentPane);
>
> if (containerPoint.y < 0)
> { //we're not in the content pane
> //Could have special code to handle mouse events over
> //the menu bar or non-system window decorations, such as
> //the ones provided by the Java look and feel.
> }
> else
> {
> //The mouse event is probably over the content pane.
> //Find out exactly which component it's over.
> Component component =
> SwingUtilities.getDeepestComponentAt(
> container,
> containerPoint.x,
> containerPoint.y);
>
> if (component instanceof JButton)
> {
> //Forward events over JButtons
> Point componentPoint = SwingUtilities.convertPoint(
> getGlassPane(),
> glassPanePoint,
> component);
> if (e.getID() == e.MOUSE_RELEASED)
> {
> component.dispatchEvent(new MouseEvent(component,
> e.MOUSE_PRESSED,
> e.getWhen(),
> e.getModifiers(),
> componentPoint.x,
> componentPoint.y,
> e.getClickCount(),
> e.isPopupTrigger()));
> }
> component.dispatchEvent(new MouseEvent(component,
> e.getID(),
> e.getWhen(),
> e.getModifiers(),
> componentPoint.x,
> componentPoint.y,
> e.getClickCount(),
> e.isPopupTrigger()));
> }
> }
> }
> }
>
>
>