5

The following question is based on the following information. Scroll down to see the actual question - it refers to the console output specifically.

I have stripped out everything, and provided a simple program to reproduce the output below:

import java.awt.AWTEvent;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;

import javax.swing.JFrame;

public class Main {
    static Toolkit tk = Toolkit.getDefaultToolkit();
    static long eventMask = AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK
           + AWTEvent.KEY_EVENT_MASK;

    public static void main(String[] args) {
        tk.addAWTEventListener(new AWTEventListener() {
            @Override
            public void eventDispatched(AWTEvent e) {
                System.out.println(e.getID() + ", " + e);
            }
        }, eventMask);

        JFrame test = new JFrame();
        test.setBounds(0, 0, 100, 100);
        test.setVisible(true);
    }
}

You will see that it gives the following output in the console:

500, java.awt.event.MouseEvent[MOUSE_CLICKED,(71,54),absolute(71,54),button=1,modifiers=Button1,clickCount=1] on frame0
501, java.awt.event.MouseEvent[MOUSE_PRESSED,(71,54),absolute(71,54),button=1,modifiers=Button1,extModifiers=Button1,clickCount=1] on frame0
506, java.awt.event.MouseEvent[MOUSE_DRAGGED,(70,54),absolute(70,54),modifiers=Button1,extModifiers=Button1,clickCount=0] on frame0
502, java.awt.event.MouseEvent[MOUSE_RELEASED,(67,54),absolute(67,54),button=1,modifiers=Button1,clickCount=1] on frame0
503, java.awt.event.MouseEvent[MOUSE_MOVED,(67,55),absolute(67,55),clickCount=0] on frame0
503, java.awt.event.MouseEvent[MOUSE_MOVED,(65,91),absolute(65,91),clickCount=0] on frame0
505, java.awt.event.MouseEvent[MOUSE_EXITED,(65,92),absolute(65,92),button=0,clickCount=0] on frame0

My question is - how can I get access to in individual entities in this

[MOUSE_RELEASED,(67,54),absolute(67,54),button=1,modifiers=Button1,clickCount=1]

without parsing out the data?

I need global event listeners in my situation. I've never used them before so I'm sure it's something I'm overlooking. Related question (where this all spawned from), Java check mouse button state

Community
  • 1
  • 1
Jay
  • 18,959
  • 11
  • 53
  • 72

2 Answers2

7

Just check e instanceof MouseEvent and get all the parameters from the MouseEvent

public void eventDispatched(AWTEvent e) { 
  if (e instanceof MouseEvent) {
    MouseEvent  me=(MouseEvent)e;
  } 
}

long KEY_EVENTS = AWTEvent.KEY_EVENT_MASK;
long MOUSE_EVENTS = AWTEvent.MOUSE_EVENT_MASK;
long MOUSE_MOTION_EVENTS = AWTEvent.MOUSE_MOTION_EVENT_MASK;
mKorbel
  • 109,525
  • 20
  • 134
  • 319
StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • When I try this, it throws the error: _Incompatible conditional operand types AWTEvent and MouseEvent_ (on your line 2) – Jay Dec 21 '11 at 07:46
  • Thanks for provoking mKorbel's answer. A combination of this answer and that one solved my problem. Wish I could accept two answers! – Jay Dec 21 '11 at 09:28
  • @TrevorSenior should work exactly as shown by Stan - if you need to mix-in anything, something else is wrong in your context which needs your attention :-) – kleopatra Dec 21 '11 at 11:14
6

code for correct suggestion by @StanislavL

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class ClosingFrame extends JFrame {

    private static final long serialVersionUID = 1L;
    private AWTEventListener awt;

    public ClosingFrame() {
        setName("Dialog ... ClosingFrame");
        JButton b = new JButton("<html>close dialog.  <br>enter this button from <br>"
                + "the top to get the <br>hoverhelp close enough<br>to cause problems<html>");
        b.setName("buttonToPress");
        b.setToolTipText("<html>111111111111111111111"
                + "1111111111111111111111111111111111111111111<br></html>");
        b.addActionListener(closeActionListener);
        add(b);
        JPopupMenu menu = new JPopupMenu();
        menu.add("some item ...........");
        menu.add("another one ..........");
        b.setComponentPopupMenu(menu);
        pack();
        setVisible(true);
    }

    private AWTEventListener createAWTWindowListener() {
        AWTEventListener awt1 = new AWTEventListener() {

            @Override
            public void eventDispatched(AWTEvent e) {
                if (MouseEvent.MOUSE_EXITED == e.getID()) {
                    MouseEvent event = (MouseEvent) e;
                    String name = event.getComponent().getName();
                    System.out.println("got mouseExited: " + name);
                    awtMouseExited(event);
                }
            }
        };
        return awt1;
    }

    private void awtMouseExited(MouseEvent event) {
        Point point = SwingUtilities.convertPoint(event.getComponent(), event.getPoint(), this);
        if (!getBounds().contains(point)) {
//            if (!getRootPane().getVisibleRect().contains(point)) {
            dispose();
        }
    }

    private void installHoverListeners() {
        if (awt != null) {
            return;
        }
        awt = createAWTWindowListener();
        Toolkit.getDefaultToolkit().addAWTEventListener(awt, AWTEvent.MOUSE_EVENT_MASK);
    }

    private void uninstallHoverListeners() {
        if (awt == null) {
            return;
        }
        Toolkit.getDefaultToolkit().removeAWTEventListener(awt);
        awt = null;
    }

    @Override
    public void addNotify() {
        super.addNotify();
        installHoverListeners();
    }

    @Override
    public void removeNotify() {
        uninstallHoverListeners();
        super.removeNotify();
    }
    private ActionListener closeActionListener = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            dispose();
        }
    };

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                ClosingFrame closingFrame = new ClosingFrame();
            }
        });
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • Working on implementing it in my code at the moment. I've learnt some other things from this - thanks! – Jay Dec 21 '11 at 09:21