17

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.

asked Apr 19, 2010 at 15:35

5 Answers 5

27

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);
 }
}
answered Apr 19, 2010 at 15:38
Sign up to request clarification or add additional context in comments.

3 Comments

thanks. i feel sorry to be asking this but im just beginning with java. i was wondering how to implement MouseAdapter separately so that the code is not inline? i cannot extend as im already extending JFrame.
You could create a separate class say, MyMouseListeneras: public class MyMouseListener extends MouseAdapter(){ public void mosuePressed(MouseEvent me){ //whatever you want to happen } } and then add that as mouse listener in your UI code as: addMouseListener(new MyMouseListener()); Now one way to make your UI elements available to your newly implemented MyMouseListener is via constructor parameters. If you want details/code, let me know.
thats great. i have done exactly as you said and have this functionality now. thank you.
8

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.

answered Apr 19, 2010 at 15:38

Comments

5

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

answered Apr 19, 2010 at 15:37

Comments

1

You can inherit from java.awt.event.MouseAdapter and only override the methods for the event(s) you are interested in.

answered Apr 19, 2010 at 15:39

Comments

1

some example of mouse event clicked,

in the same way you can use mousePressed or other mouse events

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Work1 extends JFrame{
 private JPanel panelNew;
 public Work1(){
 super("Work 1");
 // create Panel
 panelNew = new JPanel();
 panelNew.setLayout(null);
 panelNew.setBackground(Color.cyan );
 add(panelNew);
 // create Button
 JButton btn = new JButton("click me");
 // position and size of a button
 btn.setBounds(100, 50, 150, 30);
 panelNew.add(btn);
 // add event to button
 btn.addMouseListener(new MouseAdapter() { 
 public void mouseClicked(MouseEvent me) { 
 // change text of button after click
 if (btn.getText() == "abraCadabra"){
 btn.setText("click me again") ;
 }
 else btn.setText("abraCadabra");
 } 
 }); 
 }
 public static void main(String[] args){
 Work1 go1 = new Work1();
 go1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 go1.setSize(320,200);
 go1.setVisible(true); 
 }
}
answered Jul 28, 2014 at 13:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.