Connecting Tech Pros Worldwide Forums | Help | Site Map

Problem in mouselistener and windowllistener event handling

Newbie
 
Join Date: Jul 2009
Location: delhi, india
Posts: 16
#1: 4 Weeks Ago
hello all,

Just for learning purpose I am making a gui in which wherever a user click on window a text " mouse clicked" will appear at current position of mouse pointer.

I have used listener interfaces.

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.applet.*;
  4.  
  5. public class MouseEvents extends Frame implements MouseListener,WindowListener{
  6.  
  7.      String ss = "mouse clicked";
  8.     int mousex,mousey;
  9.  
  10.     public void windowClosed(WindowEvent e) {
  11.         throw new UnsupportedOperationException("Not supported yet.");
  12.     }
  13.  
  14.     public void windowDeiconified(WindowEvent e) {
  15.        throw new UnsupportedOperationException("Not supported yet.");
  16.     }
  17.  
  18.     public void windowIconified(WindowEvent e) {
  19.         throw new UnsupportedOperationException("Not supported yet.");
  20.     }
  21.  
  22.     public void windowOpened(WindowEvent e) {
  23.         throw new UnsupportedOperationException("Not supported yet.");
  24.     }
  25.  
  26.     public void windowDeactivated(WindowEvent e) {
  27.         throw new UnsupportedOperationException("Not supported yet.");
  28.     }
  29.  
  30.     public void windowActivated(WindowEvent e) {
  31.         throw new UnsupportedOperationException("Not supported yet.");
  32.     }
  33.  
  34.     public void windowClosing(WindowEvent e) {
  35.         System.exit(0);
  36.     }
  37.  
  38.     public void mouseExited(MouseEvent e) {
  39.         throw new UnsupportedOperationException("Not supported yet.");
  40.     }
  41.  
  42.     public void mousePressed(MouseEvent e) {
  43.         throw new UnsupportedOperationException("Not supported yet.");
  44.     }
  45.  
  46.  
  47.     public void mouseClicked(MouseEvent e) {
  48.     mousex = e.getX();
  49.     mousey = e.getY();
  50.         repaint();
  51.     }
  52.     @override
  53.     public void paint(Graphics g){
  54.         g.drawString(ss, mousex, mousey);
  55.     }
  56.     public void mouseEntered(MouseEvent e) {
  57.         throw new UnsupportedOperationException("Not supported yet.");
  58.     }
  59.  
  60.     public void mouseReleased(MouseEvent e) {
  61.         throw new UnsupportedOperationException("Not supported yet.");
  62.     }
  63.  
  64.     public void MouseEvents(){
  65.         addMouseListener(this);
  66.         addWindowListener(this);
  67.     }
  68. public static void main(String args[]){
  69.     MouseEvents ee = new MouseEvents();
  70.     ee.setSize(500, 500);
  71.     ee.setVisible(true);
  72.  
  73. }
  74.  
  75. }

But this code is not working according to expectations. I am not getting "mouse clicked" text and window doesn't get closed. I think my listener is not getting registered in my program. Also I have to use @Override at line 52 other wise netbeans is showing message here "multiple annotation here[2] - click to cycle."

Reply