0

Ok, I am trying to use MouseListener for the first time, but I'm not having much luck. My program compiles fine but the MouseListener Events don't seem to do anything. Here is my code:

import java.awt.color.*;
import java.awt.font.*;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class yo implements MouseListener {

Image image;
JFrame frame = new JFrame();
JLabel heloo = new JLabel("yo");
JPanel panel = new JPanel()
{
    @Override
    public void paintComponent(Graphics g)
    {
        //super.paintComponent(g);
        //ImageIcon i = new ImageIcon("hi.jpg");
        //image = i.getImage();
        //g.drawImage(image,150,150,null);
        //g.drawString("Hello",100,100);
        //g.drawString("Hi",50,50);
    }
};


public yo()
{
    frame.add(panel);
    frame.setTitle("Hello");
    frame.setSize(500,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
    panel.add(heloo);
}

public void mouseClicked (MouseEvent Event)
{
    heloo.setText("Hi");
    System.out.println("Hi");
}
public void mouseEntered (MouseEvent Event)
{System.out.println("Hi");}
public void mouseExited (MouseEvent Event)
{}
public void mousePressed (MouseEvent Event)
{}
public void mouseReleased (MouseEvent Event)
{}

public static void main(String[] args)
{
    new yo();
}
}

By not doing anything I mean that the system doesn't output text to command line or change the JLabel.

Any help on how to get it to work would be great, thanks.

p.s. I'm a noob so, be nice.

2
  • I don't see anywhere that you're register the mouse listener? Try something like heloo.addMouseListener(new yo()); Jun 21, 2013 at 23:54
  • I just got it to work, do not worry. I just added frame.addMouseListener(this); Jun 21, 2013 at 23:57

3 Answers 3

2

Read the Swing tutorial on How to Write a MouseListener.

You didn't add the listener to any component.

0

put

frame.addMouseListener(this);

in the constructor

1
  • 1
    That won't work. The class that implements the MouseListener is not a Component.
    – camickr
    Jun 21, 2013 at 23:58
0

You made a yo a MouseListener, but you didn't add it to anything.

You need to use .addMouseListener(this); on each component you want to listen to.

e.g.

frame.addMouseListener(this), or if in a static method frame.addMouseListener(myInstanceOfYo);

try

public yo()
{
    frame.add(panel);
    frame.setTitle("Hello");
    frame.setSize(500,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setLocationRelativeTo(null);
    panel.add(heloo);


    frame.addMouseListener(this);


}

Edit:

I would also suggest you change your test text in each MouseListener method to be unique, so it's easier to see which was called, and when. Also, make the parameter name start with a lower case letter (Event becomes event), it's just good practice.

i.e.

public void mouseClicked (MouseEvent event)
{
    heloo.setText("Hi");
    System.out.println("Clicked.");
}
public void mouseEntered (MouseEvent event)
{
    System.out.println("Entered.");
}
public void mouseExited (MouseEvent event)
{
    System.out.println("Exited.");
}
public void mousePressed (MouseEvent event)
{
    System.out.println("Pressed.");
}
public void mouseReleased (MouseEvent event)
{
    System.out.println("Released.");
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.