Reputation: 5296
I have a JFrame with a lot of subcomponents and subpanels with different functions. I have added mouseListeners to some of them.
I want to be able to call a function when a mousebutton is clicked in the frame, regardless of what component is at focus. I dont want to add mouselisteners to all of the components. Much like keyEventDispacher, I am useing it for the keyboard.
I have looked at this Dispatch MouseEvent but I wasn't able to work it out. Could anyone give me a small example to demonstrate how it works?
Upvotes: 1
Views: 2672
Reputation: 10153
Here is a simple example of global mouse listener:
public static void main ( String[] args )
{
final JFrame frame = new JFrame ();
// Some content
frame.getContentPane ().setLayout ( new FlowLayout ( FlowLayout.CENTER, 5, 5 ) );
frame.getContentPane ().add ( new JButton ( "Test" ) );
frame.getContentPane ().add ( new JLabel ( "Test" ) );
frame.getContentPane ().add ( new JTextField ( "Test" ) );
// Global mouse listener
final AWTEventListener listener = new AWTEventListener ()
{
public void eventDispatched ( AWTEvent event )
{
// Event and component that recieved that event
MouseEvent me = ( MouseEvent ) event;
Component c = me.getComponent ();
// Ignoring mouse events from any other frame
if ( SwingUtilities.getWindowAncestor ( c ) == frame )
{
if ( event.getID () == MouseEvent.MOUSE_PRESSED )
{
System.out.println ( "Mouse pressed on " + c.getClass ().getCanonicalName () );
}
if ( event.getID () == MouseEvent.MOUSE_RELEASED )
{
System.out.println ( "Mouse released on " + c.getClass ().getCanonicalName () );
}
if ( event.getID () == MouseEvent.MOUSE_ENTERED )
{
System.out.println ( "Mouse entered " + c.getClass ().getCanonicalName () );
}
if ( event.getID () == MouseEvent.MOUSE_EXITED )
{
System.out.println ( "Mouse exited " + c.getClass ().getCanonicalName () );
}
}
}
};
Toolkit.getDefaultToolkit ().addAWTEventListener ( listener, AWTEvent.MOUSE_EVENT_MASK );
frame.pack ();
frame.setLocationRelativeTo ( null );
frame.setVisible ( true );
}
Be aware that this listener will inform you about every mouse event inside any frame/dialog/window/popup. That is why you should limit your actions to some specific frame like i did in the example in case you don't need events from other frames/windows/...
By the way, you can easily listen to some other events globally (for example key events) using the same approach. For example - use the AWTEvent.KEY_EVENT_MASK key instead of AWTEvent.MOUSE_EVENT_MASK and cast AWTEvent to KeyEvent instead of MouseEvent.
Upvotes: 2
Reputation: 347334
This would be a world of mess and pain at the best of times...
Check here Global Event Listeners with AWTEventListener & how to pull MouseEvent's from it for an alternative concept. Basically it's a global mouse listener. You need to do a lot of work to get this to work for you (checking the children belong to the parent etc), but it does work well.
Upvotes: 1
Reputation: 33544
Try using MouseEvent Class
, with static method MOUSE_CLICKED
See here for further details:
http://docs.oracle.com/javase/6/docs/api/java/awt/event/MouseEvent.html
See this example from roseindia
import java.awt.*;
import java.awt.event.*;
public class MouseClick {
Label lbl;
public static void main(String[] args) {
MouseClick MC = new MouseClick();
}
public MouseClick(){
Frame f = new Frame("Checking the mouse click");
Panel p = new Panel();
Button button = new Button("Click Me");
button.addMouseListener(new MyMouseListener());
p.add(button, BorderLayout.NORTH);
f.add(p,BorderLayout.NORTH);
lbl = new Label("Roseindia.net");
f.add(lbl, BorderLayout.CENTER);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
f.setSize(400,400);
f.setVisible(true);
}
public class MyMouseListener extends MouseAdapter{
public void mouseClicked(MouseEvent me){
String str = lbl.getText();
if (str.equals("Roseindia.net")){
lbl.setText("You have clicke the button.");
}
else if (str.equals("You have clicke the button.")){
lbl.setText("Roseindia.net");
}
}
}
}
See this example from Oracle
public class MouseEventDemo ... implements MouseListener {
//where initialization occurs:
//Register for mouse events on blankArea and the panel.
blankArea.addMouseListener(this);
addMouseListener(this);
...
public void mousePressed(MouseEvent e) {
saySomething("Mouse pressed; # of clicks: "
+ e.getClickCount(), e);
}
public void mouseReleased(MouseEvent e) {
saySomething("Mouse released; # of clicks: "
+ e.getClickCount(), e);
}
public void mouseEntered(MouseEvent e) {
saySomething("Mouse entered", e);
}
public void mouseExited(MouseEvent e) {
saySomething("Mouse exited", e);
}
public void mouseClicked(MouseEvent e) {
saySomething("Mouse clicked (# of clicks: "
+ e.getClickCount() + ")", e);
}
void saySomething(String eventDescription, MouseEvent e) {
textArea.append(eventDescription + " detected on "
+ e.getComponent().getClass().getName()
+ "." + newline);
}
}
Upvotes: 0