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
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;
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();
}
});
}
}