Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MouseListener on JFrame

I want to be notified of mouse events (specifically the mouse entered and exited events) on my JFrame. But when i add a mouselistener to it i get the events on the borders of the frame not the entire frame with it's contents.

Any ideas as to why?

EDIT : Or at least do you have an alternative? I want a "gloabal" way to catch mouse events on the JFrame. Maybe a mouselistener is not the answer.

like image 821
Savvas Dalkitsis Avatar asked Sep 10 '09 22:09

Savvas Dalkitsis


2 Answers

You can get all events and check if their source is a component in the JFrame.

See Toolkit.addAWTEventListener

like image 187
ykaganovich Avatar answered Sep 29 '22 02:09

ykaganovich


There is an invisible component that overlays the whole GUI, the "glass pane". You can attach your listeners to that. Example:

JFrame frame = new JFrame();
Component glassPane = frame.getGlassPane();
glassPane.addMouseListener(myListener);

If you want your intercepted events to pass through to the underlying components, you can redispatch them. For example:

public void mouseMoved(MouseEvent e) {
    redispatchMouseEvent(e, false);
}
like image 24
SingleShot Avatar answered Sep 29 '22 04:09

SingleShot