Tallal Butt Tallal Butt - 1 year ago 133
Java Question

Mouse Adapter program not working

public class MouseDetailsFrame extends JFrame {

private String details;
private JLabel statusbar;

public MouseDetailsFrame(){


statusbar= new JLabel("Click The Mouse");
add(statusbar, BorderLayout.SOUTH);
addMouseListener(new MouseClickHandler());

private class MouseClickHandler extends MouseAdapter{

public void MouseClicked(MouseEvent event){
int xPos= event.getX();// has no use anywhere, i wonder why that is
int yPos= event.getY();// same

details= String.format(" clicked %d times:", event.getClickCount());

if (event.isMetaDown())
details = String.format(" clicked %d times:"+"with right mouse", event.getClickCount());
else if(event.isAltDown())
details=String.format(" clicked %d times:"+"with middle mouse", event.getClickCount());
details =String.format(" clicked %d times:"+ "with left mouse", event.getClickCount());



// nothing happens when i run it through the main string. A window just shows up the statusbar doesnt show how many time i click or if i clicked with left or right mouse button. Just give me hint please, i want to do this myself.

Answer Source
  1. Add an @Override annotation to your MouseClicked method - what do you notice?
    This is one of the cases in which @Override spares you from grief.

  2. Go and check the javadoc for the mouseClicked method: do you notice the spelling difference?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download