Tallal Butt Tallal Butt - 2 months ago 16
Java Question

Mouse Adapter program not working

public class MouseDetailsFrame extends JFrame {

private String details;
private JLabel statusbar;

public MouseDetailsFrame(){

super("MouseFrame");

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());
else
details =String.format(" clicked %d times:"+ "with left mouse", event.getClickCount());

statusbar.setText(details);
}

}
}


// 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
  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?