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) {
}
publ
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.