+ Reply to Thread
Results 1 to 4 of 4

Thread: Java: MouseListener

  1. #1
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    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.

    Code:
    package tutorials;
    
    import javax.swing.JFrame;
    
    public class MouseMethods {
    
    	public MouseMethods() {
    		JFrame frame = new JFrame("MouseMethods");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(500, 500);
    
    		
    		frame.setVisible(true);
    		
    	}
    
    	public static void main(String args[]) {
    		new MouseMethods();
    	}
    	
    }
    Next in the class header we will implement and import the java.awt.event package.


    Code:
    package tutorials;
    
    import java.awt.event.*;
    import javax.swing.JFrame;
    
    public class MouseMethods implements MouseListener {
    
    	public MouseMethods() {
    		JFrame frame = new JFrame("MouseMethods");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(500, 500);
    
    		
    		frame.setVisible(true);
    		
    	}
    
    	public static void main(String args[]) {
    		new MouseMethods();
    	}
    	
    }
    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:

    Code:
    package tutorials;
    
    import java.awt.event.*;
    import javax.swing.JFrame;
    
    public class MouseMethods implements MouseListener {
    
    	public MouseMethods() {
    		JFrame frame = new JFrame("MouseMethods");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(500, 500);
    
    		
    		frame.setVisible(true);
    		
    	}
    
    	public void mouseClicked(MouseEvent e) {
    		System.out.println("The frame was clicked.");
    		
    	}
    
    	public void mouseEntered(MouseEvent e) {
    		System.out.println("The mouse entered the frame.");
    		
    	}
    
    	public void mouseExited(MouseEvent e) {
    		System.out.println("The mouse exited the frame.");
    		
    	}
    
    	public void mousePressed(MouseEvent e) {
    		System.out.println("The left mouse button was pressed.");
    		
    	}
    
    	public void mouseReleased(MouseEvent e) {
    		System.out.println("The left mouse button was released.");
    		
    	}
    	
    	public static void main(String args[]) {
    		new MouseMethods();
    	}
    	
    }
    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."

    Code:
    package tutorials;
    
    
    import java.awt.event.*;
    import javax.swing.JFrame;
    
    public class MouseMethods implements MouseListener {
    
    	public MouseMethods() {
    		JFrame frame = new JFrame("MouseMethods");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(500, 500);
    		frame.addMouseListener(this);
    		
    		frame.setVisible(true);
    		
    	}
    
    	public void mouseClicked(MouseEvent e) {
    		System.out.println("The frame was clicked.");
    		
    	}
    
    	public void mouseEntered(MouseEvent e) {
    		System.out.println("The mouse entered the frame.");
    		
    	}
    
    	public void mouseExited(MouseEvent e) {
    		System.out.println("The mouse exited the frame.");
    		
    	}
    
    	public void mousePressed(MouseEvent e) {
    		System.out.println("The left mouse button was pressed.");
    		
    	}
    
    	public void mouseReleased(MouseEvent e) {
    		System.out.println("The left mouse button was released.");
    		
    	}
    	
    	public static void main(String args[]) {
    		new MouseMethods();
    	}
    	
    }
    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:

    Code:
    package tutorials;
    
    import java.awt.FlowLayout;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    public class MouseMethods implements MouseListener {
    
    
    	private JLabel label = new JLabel("This is a JLabel");
    	private JButton button = new JButton("This is a JButton");
    	
    	public MouseMethods() {
    		JFrame frame = new JFrame("MouseMethods");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setLayout(new FlowLayout());
    		
    		label.addMouseListener(this);
    		button.addMouseListener(this);
    		
    		frame.add(label);
    		frame.add(button);
    		frame.setVisible(true);
    		frame.pack();
    		
    	}
    
    	public void mouseClicked(MouseEvent e) {
    		if(e.getSource().equals(button)){
    			System.out.println("The JButton was clicked...");
    		} else if(e.getSource().equals(label)){
    			System.out.println("The JLabel was clicked...");
    		} else {
    			System.out.println("Something else was clicked...");
    		}
    		
    	}
    
    	public void mouseEntered(MouseEvent e) {
    
    	}
    
    	public void mouseExited(MouseEvent e) {
    		
    	}
    
    	public void mousePressed(MouseEvent e) {
    		
    	}
    
    	public void mouseReleased(MouseEvent e) {
    		
    	}
    	
    	public static void main(String args[]) {
    		new MouseMethods();
    	}
    	
    }
    Originally posted as MoustListener
    Last edited by John; 08-01-2010 at 09:33 AM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    ElPolloDiablo is offline Newbie
    Join Date
    Sep 2007
    Location
    Oudenaarde, Belgium
    Posts
    1
    Rep Power
    0
    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:
    [HIGHLIGHT="Java5"]
    package tutorials;
    import java.awt.FlowLayout;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseAdapter;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    public class MouseMethods extends MouseAdapter {

    private JLabel label = new JLabel("This is a JLabel");
    private JButton button = new JButton("This is a JButton");

    public MouseMethods() {
    JFrame frame = new JFrame("MouseMethods");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setLayout(new FlowLayout());
    label.addMouseListener(this);
    button.addMouseListener(this);

    frame.add(label);
    frame.add(button);
    frame.setVisible(true);
    frame.pack();
    }

    public void mouseClicked(MouseEvent e) {
    if(e.getSource().equals(button)){
    System.out.println("The JButton was clicked...");
    } else if(e.getSource().equals(label)){
    System.out.println("The JLabel was clicked...");
    } else {
    System.out.println("Something else was clicked...");
    }
    }

    public static void main(String args[]) {
    new MouseMethods();
    }
    } [/HIGHLIGHT]

  4. #3
    Join Date
    Feb 2008
    Location
    chennai
    Posts
    1
    Rep Power
    0
    simply superb

  5. #4
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20
    Thanks, glad I could help

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 2 users browsing this thread. (0 members and 2 guests)

Similar Threads

  1. Replies: 2
    Last Post: 05-01-2011, 12:53 AM
  2. ActionListener and MouseListener
    By toto_7 in forum Java Help
    Replies: 11
    Last Post: 03-25-2011, 09:20 AM
  3. Replies: 6
    Last Post: 06-14-2010, 04:11 PM
  4. Button + MouseListener?
    By darkaza in forum Java Help
    Replies: 0
    Last Post: 01-10-2009, 10:37 PM
  5. Replies: 0
    Last Post: 10-19-2007, 09:57 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts