Lost Password?

  #1 (permalink)  
Old 09-23-2007, 10:01 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 19
Posts: 2,402
Last Blog:
Object Oriented Design...
Rep Power: 50
John is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of lightJohn is a glorious beacon of light
Send a message via AIM to John
Default Java: MouseListener

Any program that actually does something is event driven. With out events, your program is entirely static and probably does nothing. While some events might be caused by an internal timer, most events are caused by the user. This tutorial will show you how to use the methods given to use my the MouseListener class.

First we are going to create a simple swing GUI. Since this tutorial already assumes you know how to do that, I won't go into details on this subject.

Java Code:
  1. package tutorials;
  2.  
  3. import javax.swing.JFrame;
  4.  
  5. public class MouseMethods {
  6.  
  7.     public MouseMethods() {
  8.         JFrame frame = new JFrame("MouseMethods");
  9.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  10.         frame.setSize(500, 500);
  11.  
  12.        
  13.         frame.setVisible(true);
  14.        
  15.     }
  16.  
  17.     public static void main(String args[]) {
  18.         new MouseMethods();
  19.     }
  20.    
  21. }

Next in the class header we will implement and import the java.awt.event package.


Java Code:
  1. package tutorials;
  2.  
  3. import java.awt.event.*;
  4. import javax.swing.JFrame;
  5.  
  6. public class MouseMethods implements MouseListener {
  7.  
  8.     public MouseMethods() {
  9.         JFrame frame = new JFrame("MouseMethods");
  10.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  11.         frame.setSize(500, 500);
  12.  
  13.        
  14.         frame.setVisible(true);
  15.        
  16.     }
  17.  
  18.     public static void main(String args[]) {
  19.         new MouseMethods();
  20.     }
  21.    
  22. }

Since we are implementing the class, we need to use all the methods provided to us by MouseListener which, in no specific order are: mouseClicked(), mouseEntered(), mouseExited(), mousePressed(), and mouseReleased(). The difference between mouseClicked() and mousePressed()/mouseReleased() are minimal, its just that mouseClicked() is a mouse press and mouse release, which mousePressed() and mouseReleased just listen for the mouse being pressed and released respectively. So after implementing the methods or class looks something like this:

Java Code:
  1. package tutorials;
  2.  
  3. import java.awt.event.*;
  4. import javax.swing.JFrame;
  5.  
  6. public class MouseMethods implements MouseListener {
  7.  
  8.     public MouseMethods() {
  9.         JFrame frame = new JFrame("MouseMethods");
  10.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  11.         frame.setSize(500, 500);
  12.  
  13.        
  14.         frame.setVisible(true);
  15.        
  16.     }
  17.  
  18.     public void mouseClicked(MouseEvent e) {
  19.         System.out.println("The frame was clicked.");
  20.        
  21.     }
  22.  
  23.     public void mouseEntered(MouseEvent e) {
  24.         System.out.println("The mouse entered the frame.");
  25.        
  26.     }
  27.  
  28.     public void mouseExited(MouseEvent e) {
  29.         System.out.println("The mouse exited the frame.");
  30.        
  31.     }
  32.  
  33.     public void mousePressed(MouseEvent e) {
  34.         System.out.println("The left mouse button was pressed.");
  35.        
  36.     }
  37.  
  38.     public void mouseReleased(MouseEvent e) {
  39.         System.out.println("The left mouse button was released.");
  40.        
  41.     }
  42.    
  43.     public static void main(String args[]) {
  44.         new MouseMethods();
  45.     }
  46.    
  47. }

The last step we must do is add a mouseListener to an object. Most J-Objects [JLabels, JFrames, JButton, ect...] I am going to add a mouse listener to or main frame "frame."

Java Code:
  1. package tutorials;
  2.  
  3.  
  4. import java.awt.event.*;
  5. import javax.swing.JFrame;
  6.  
  7. public class MouseMethods implements MouseListener {
  8.  
  9.     public MouseMethods() {
  10.         JFrame frame = new JFrame("MouseMethods");
  11.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  12.         frame.setSize(500, 500);
  13.         frame.addMouseListener(this);
  14.        
  15.         frame.setVisible(true);
  16.        
  17.     }
  18.  
  19.     public void mouseClicked(MouseEvent e) {
  20.         System.out.println("The frame was clicked.");
  21.        
  22.     }
  23.  
  24.     public void mouseEntered(MouseEvent e) {
  25.         System.out.println("The mouse entered the frame.");
  26.        
  27.     }
  28.  
  29.     public void mouseExited(MouseEvent e) {
  30.         System.out.println("The mouse exited the frame.");
  31.        
  32.     }
  33.  
  34.     public void mousePressed(MouseEvent e) {
  35.         System.out.println("The left mouse button was pressed.");
  36.        
  37.     }
  38.  
  39.     public void mouseReleased(MouseEvent e) {
  40.         System.out.println("The left mouse button was released.");
  41.        
  42.     }
  43.    
  44.     public static void main(String args[]) {
  45.         new MouseMethods();
  46.     }
  47.    
  48. }

And thats all you need to do. Now since each MouseListener method (mouseClicked, MouseEntered, ect..) is passed a MouseEvent "e" you can expand the capabilities of the methods. For example if you wanted to check which object is being clicked, it might look something like this:

Java Code:
  1. package tutorials;
  2.  
  3. import java.awt.FlowLayout;
  4. import java.awt.event.MouseEvent;
  5. import java.awt.event.MouseListener;
  6. import javax.swing.JButton;
  7. import javax.swing.JFrame;
  8. import javax.swing.JLabel;
  9.  
  10. public class MouseMethods implements MouseListener {
  11.  
  12.  
  13.     private JLabel label = new JLabel("This is a JLabel");
  14.     private JButton button = new JButton("This is a JButton");
  15.    
  16.     public MouseMethods() {
  17.         JFrame frame = new JFrame("MouseMethods");
  18.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19.         frame.setLayout(new FlowLayout());
  20.        
  21.         label.addMouseListener(this);
  22.         button.addMouseListener(this);
  23.        
  24.         frame.add(label);
  25.         frame.add(button);
  26.         frame.setVisible(true);
  27.         frame.pack();
  28.        
  29.     }
  30.  
  31.     public void mouseClicked(MouseEvent e) {
  32.         if(e.getSource().equals(button)){
  33.             System.out.println("The JButton was clicked...");
  34.         } else if(e.getSource().equals(label)){
  35.             System.out.println("The JLabel was clicked...");
  36.         } else {
  37.             System.out.println("Something else was clicked...");
  38.         }
  39.        
  40.     }
  41.  
  42.     public void mouseEntered(MouseEvent e) {
  43.  
  44.     }
  45.  
  46.     public void mouseExited(MouseEvent e) {
  47.        
  48.     }
  49.  
  50.     public void mousePressed(MouseEvent e) {
  51.        
  52.     }
  53.  
  54.     public void mouseReleased(MouseEvent e) {
  55.        
  56.     }
  57.    
  58.     public static void main(String args[]) {
  59.         new MouseMethods();
  60.     }
  61.    
  62. }
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum | My Blog
Chat with other CodeCall members on IRC; connect to irc.codecall.net and join #codecall
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 09-24-2007, 09:07 AM
ElPolloDiablo ElPolloDiablo is offline
Newbie
 
Join Date: Sep 2007
Location: Oudenaarde, Belgium
Age: 25
Posts: 1
Rep Power: 0
ElPolloDiablo is on a distinguished road
Send a message via MSN to ElPolloDiablo
Default

It might also be interesting to explain its derived adapter class.

The MouseListener interface has an abstract class implementation called MouseAdapter.

The advantage of using the adapter class instead of the interface is that when you only need one method (say, mouseClicked, as in the final example), you don't need to implement the other ones.

The code, when using MouseAdapter, would then look like this:
Java5 Code:
  1. package tutorials;
  2. import java.awt.FlowLayout;
  3. import java.awt.event.MouseEvent;
  4. import java.awt.event.MouseAdapter;
  5. import javax.swing.JButton;
  6. import javax.swing.JFrame;
  7. import javax.swing.JLabel;
  8. public class MouseMethods extends MouseAdapter {
  9.  
  10.           private JLabel label = new JLabel("This is a JLabel");
  11.           private JButton button = new JButton("This is a JButton");
  12.  
  13.           public MouseMethods() {
  14.               JFrame frame = new JFrame("MouseMethods");
  15.               frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  16.               frame.setLayout(new FlowLayout());
  17.               label.addMouseListener(this);
  18.               button.addMouseListener(this);
  19.  
  20.               frame.add(label);
  21.               frame.add(button);
  22.               frame.setVisible(true);
  23.               frame.pack();
  24.           }
  25.        
  26.           public void mouseClicked(MouseEvent e) {
  27.               if(e.getSource().equals(button)){
  28.                   System.out.println("The JButton was clicked...");
  29.               } else if(e.getSource().equals(label)){
  30.                   System.out.println("The JLabel was clicked...");
  31.               } else {
  32.                   System.out.println("Something else was clicked...");
  33.               }
  34.           }
  35.  
  36.           public static void main(String args[]) {
  37.               new MouseMethods();
  38.           }
  39. }
Digg this Post!Add Post to del.icio.us