20-CS-694 Advanced Programming Techniques Spring 2012
Mouse Actions

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

       Previous     Next         Applet Source     All lectures

Acting on Mouse Click

The MouseListener interface can supply mouse information in the event of mouse clicks. But the MouseListener interface requires implementing five methods, only one of which is needed in this example. So, an inner MouseAdapter class is created here with just the mousePressed method overridden and addMouseListener takes a reference to an object of that class as argument (instead of this). Every click of the mouse in the MousePad JPanel adds 1 to a count that is displayed in a JTextField in the north section of the JFrame.

Instructions: Move the mouse cursor and periodically click the mouse button.
The upper right text field records the count. Edit the count text field, replacing
the number showing with a string such as "Hello World." The next time the
mouse is clicked a NumberFormatException is raised. This is observed in the
status line at the bottom of Netscape.

// Acting on a mouse click - what happens if count is set to "Hello"?
import java.awt.*;         // for Panel, TextField, BorderLayout
import java.awt.event.*;   // for MouseMotionListener
import javax.swing.*;
import java.applet.*;


class MousePad extends JPanel implements MouseMotionListener {
   JTextField mouse_x, mouse_y, count;
   
   MouseAdapter ma = new MouseAdapter() {
      public void mousePressed (MouseEvent event) {
         count.setText(String.valueOf(Integer.parseInt(count.getText())+1));
      }
   };
   
   MousePad (JTextField mouse_x, JTextField mouse_y, JTextField count) {
      this.mouse_x = mouse_x;
      this.mouse_y = mouse_y;
      this.count = count;
      addMouseMotionListener(this);
      addMouseListener(ma);
      mouse_x.setEditable(false);
      mouse_x.setBackground(Color.white);
      mouse_y.setEditable(false);
      mouse_y.setBackground(new Color(162, 195, 255));
   }
   
   public void mouseMoved (MouseEvent event) {
      mouse_x.setText(String.valueOf(event.getX()));
      mouse_y.setText(String.valueOf(event.getY()));
   }

   public void mouseDragged (MouseEvent event) { }
}

class ShowMousePressedFrame extends JFrame {
   JTextField mouse_x, mouse_y, count;
   
   public ShowMousePressedFrame () {
      setLayout(new BorderLayout());
      JPanel p = new JPanel();
      p.setLayout(new GridLayout(1,6));
      p.add(new JLabel("x :",JLabel.RIGHT));
      p.add(mouse_x = new JTextField());
      p.add(new JLabel("y :",JLabel.RIGHT));      
      p.add(mouse_y = new JTextField());
      p.add(new JLabel("count:",JLabel.RIGHT));
      p.add(count = new JTextField("0"));
      add("North", p);
      
      MousePad mp = new MousePad(mouse_x, mouse_y, count);
      add("Center",mp);
   }
}

public class ShowMousePressed extends Applet {
   public void init () {
      ShowMousePressedFrame fg = new ShowMousePressedFrame();
      fg.setSize(500,300);
      fg.setVisible(true);
   }
}