Page 1 of 1

MouseListener Rate Topic: ***** 1 Votes

#1 TFoSSDQ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 123
  • View blog
  • Posts: 253
  • Joined: 09-December 10

Posted 18 February 2011 - 07:49 AM

*
POPULAR

Ever needed something in your JFrame to work with the mouse that an ActionListener can't handle (roll over/out). Then you're going to need a MouseListener.

What is it?

Simply put, a MouseListener is an interface which allows interaction between the mouse and some event (be it a click, mouse down/up, or roll over/out) and a java Component. If a Component has a MouseListener that listens for a mouse click on it, you can control what happens when you click the Component. Here's an example:
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

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

public class MouseTest extends JFrame implements MouseListener
{
     public MouseTest()
     {
          super("MouseListener Test");

          JPanel p = new JPanel();

          JTextArea exitText = new JTextArea(2, 10);
          exitText.setText("Click me to exit");
          exitText.addMouseListener(this);

          p.add(exitText);

          getContentPane().add(p);

          setVisible(true);
     }

     public static void main(String[] args)
     {
          new MouseTest();
     }
     
     //when the mouse is clicked on the Component
     public void mouseClicked(MouseEvent e)
     {
          System.exit(0);
     }
     
     //when the mouse enters the Component
     public void mouseEntered(MouseEvent e)
     {
     }
     
     //when the mouse leaves the Component
     public void mouseExited(MouseEvent e)
     {
     }

     //when the mouse left button is held down on the Component
     public void mousePressed(MouseEvent e)
     {
     }

     //when the mouse left button is released on the Component
     public void mouseReleased(MouseEvent e)
     {
     }
}


This code is not overly complicated; we create a JPanel to hold our JTextArea which will be our component to which we add the MouseListener. As it's set up here, when we do a complete click on the JTextArea (click down and release) the program exits. I didn't define the other methods as I didn't need them for the program's purposes, but of course the methods still had to be defined because MouseListener is an interface.

If you've used KeyListeners or ActionListeners and wanted to modify the the source, you would do something like e.getSource() and cast it and store it like:
//for an ActionListener
public void actionPerformed(ActionEvent e)
{
     Component c = (Component) e.getSource();
}


Something like that. It's the same for the MouseListener. You can get the source for the event if you have multiple objects with the same MouseListener on them. It's use would be like this:
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;

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

public class MouseTest extends JFrame implements MouseListener
{
     private JTextArea exitText;
     private JTextArea printText;

     public MouseTest()
     {
          super("MouseListener Test");

          JPanel p = new JPanel();

          exitText = new JTextArea(2, 10);
          exitText.setText("Click me to exit");
          exitText.addMouseListener(this);

          printText = new JTextArea(2, 10);
          printText.setText("Click me to print \"Hello\"");
          printText.addMouseListener(this); 

          p.add(exitText);
          p.add(printText);

          getContentPane().add(p);

          setVisible(true);
     }
     
     public static void main(String[] args)
     {
          new MouseTest();
     }

     //when the mouse is clicked on the Component
     public void mouseClicked(MouseEvent e)
     {
          JTextArea temp = (JTextArea) e.getSource();
          if(temp.equals(exitText))          
               System.exit(0);
          else if(temp.equals(printText))
               System.out.println("Hello");
     }
     
     //when the mouse enters the Component
     public void mouseEntered(MouseEvent e)
     {
     }
     
     //when the mouse leaves the Component
     public void mouseExited(MouseEvent e)
     {
     }

     //when the mouse left button is held down on the Component
     public void mousePressed(MouseEvent e)
     {
     }

     //when the mouse left button is released on the Component
     public void mouseReleased(MouseEvent e)
     {
     }
}


Same MouseListener for different components with the same event (mouse click). This is very useful when you want to use only one MouseListener on multiple Components.

What I've done here with the mouseClicked method can be done with any of the other four methods depending on the situation. It's a very useful tool to work with the mouse when using components.

Happy Coding!

Is This A Good Question/Topic? 5
  • +

Replies To: MouseListener

#2 Spercetefecon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-February 11

Posted 21 February 2011 - 10:56 AM

nice...
Was This Post Helpful? 0
  • +
  • -

#3 Brainpower49  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 04-February 13

Posted 08 March 2013 - 08:08 PM

Nice tutorial, definitely worth the look. :D
Was This Post Helpful? 0
  • +
  • -

#4 Brainpower49  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 04-February 13

Posted 08 March 2013 - 08:13 PM

Nice tutorial, definitely worth the look. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1