Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java MouseListener

I have a bunch of JLabels and i would like to trap mouse click events. at the moment i am having to use:

public void mouseClicked(MouseEvent arg0) {

}

public void mouseExited(MouseEvent arg0) {

}

public void mouseEntered(MouseEvent arg0) {

}

public void mousePressed(MouseEvent arg0) {

}

public void mouseReleased(MouseEvent arg0) {

    System.out.println("Welcome to Java Programming!"); 
}

I was wondering if there is a tidier way of doing this instead of having a bunch of events I do not wish trap?

EDIT:

    class MyAdapter extends MouseAdapter {
    public void mouseClicked(MouseEvent event) {

        System.out.println(event.getComponent());
    }
}

the above works but netBeans says add @override anotation. what does this mean?

EDIT: ok got it. fixed and solved.

like image 233
iTEgg Avatar asked Apr 19 '10 15:04

iTEgg


People also ask

What is MouseListener in java?

Interface MouseListenerThe class that is interested in processing a mouse event either implements this interface (and all the methods it contains) or extends the abstract MouseAdapter class (overriding only the methods of interest).

What is the difference between Actionlistener and MouseListener?

The first difference is that A MouseEvent is a true system event, whereas an ActionEvent is a synthesized event... It is triggered by a system event. Save this answer.

What is the difference between MouseAdapter and MouseListener?

MouseListener is an Interface and MouseAdapter is an implementation of that. You can use the MouseAdapter in every place that you use a MouseListener.

Which are the interface MouseListener?

The MouseListener interface is found in java. awt. event package.


3 Answers

Use MouseAdapter()

An abstract adapter class for receiving mouse events. The methods in this class are empty. This class exists as convenience for creating listener objects. So you need to implement only the method you like such as following example:

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainClass extends JPanel {

  public MainClass() {

      addMouseListener(new MouseAdapter() { 
          public void mousePressed(MouseEvent me) { 
            System.out.println(me); 
          } 
        }); 

  }

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.getContentPane().add(new MainClass());

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.setSize(200, 200);
    frame.setVisible(true);
  }
}
like image 193
ring bearer Avatar answered Oct 13 '22 22:10

ring bearer


One could use a MouseAdapter class, which implements the MouseListener interface, so one does not need to implement all the methods.

However, by overriding the methods of interest, one can get the desired behavior. For example, if one overrides the mouseClicked method, then one can define some behavior for the mouse click event.

For example (untested code):

JLabel label = new JLabel("Hello");

label.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
        System.out.println("Clicked!");
    }
});

In the code above, the JLabel will print "Clicked!" to the console upon being clicked on.

like image 30
coobird Avatar answered Oct 14 '22 00:10

coobird


You can extend MouseAdapter instead, and just override the events you're really interested in.

like image 21
Bill the Lizard Avatar answered Oct 13 '22 22:10

Bill the Lizard