Listing 3.1 ActionEvent Example with One Button That Demonstrates Sources, Events, and Their Listeners (TActionEvent.java) /* * * */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TActionEvent extends JApplet { Container container; JLabel label; Icon speaker; public void init() { // 1. Get the handle on the applet's content pane. container = this.getContentPane(); // 2. Create a speaker icon, add it to a Swing // label, and add the label to the applet. speaker = new ImageIcon("speaker.gif"); label = new JLabel(speaker); //label.repaint(); container.add(label); // 3. Create a source (button) for the action event. JButton source = new JButton("Ring the bell!"); container.add(source, BorderLayout.SOUTH); // 4. Register the action listener with the source. source.addActionListener(new ButtonListener()); } // 5. Define the listener class. class ButtonListener implements ActionListener { // 6. Interface method that has been implemented. public void actionPerformed(ActionEvent ae) { // Ring the bell... int i=0; while (i<10) { Toolkit.getDefaultToolkit().beep(); try { Thread.currentThread().sleep(1000); } catch(InterruptedException ie) { System.out.println("Sleep Interrupted"); } i++; } } } } Listing 3.2 Low-Level Mouse Events (TMouse.java) // Demonstrates the mouse events (which are low-level events) /* * * */ import javax.swing.*; import java.awt.*; import java.awt.event.*; // 1. Create a Swing applet that implements mouse listener. public class TMouse extends JApplet implements MouseListener { Container container; int width, height; int x, y; int flag; String mouseStatus; public void init() { // 2. Get a reference to the applet's content pane. container = this.getContentPane(); // 3. Initialize the data members. x = 0; y = 0; width = 2; height = 2; // For a small square to be drawn flag = 0; // 4. Register the mouse listener with the applet. container.addMouseListener(this); } /* * NOTE: The class JApplet contains the update() method * to override the same method from the class * Component that repairs the container * background with its background color and calls * the paint() method. The update()in JApplet does not * repair the background of the container. It calls the * paint() method. You need to use an update() method that repairs * the background. */ // 5. The update() method to repair the applet's background. public void update(Graphics g) { g.setColor(this.getBackground()); g.fillRect(0, 0, // x and y coordinates getWidth(), // Get the applet's width getHeight()); // Get the applet's height paint(g); } // 6. The paint() method to paint the applet based on the flag. public void paint(Graphics g) { g.setColor(Color.blue); g.drawString("Click the Mouse Button...", 5, 20); // at x=5 & y=20 g.setColor(Color.red); if(flag == 1) g.drawString("Mouse Entered Applet!", 5, 80); else if(flag == 2) g.drawString("Mouse Exited Applet!", 5, 80); else if(flag == 3) { g.drawString("Mouse Entered Applet!", 5, 80); g.fillRect(x, y, width, height); g.drawString("Clicked Here!", x, y); } else if(flag == 4) { g.drawString("Mouse Entered Applet!", 5, 80); g.fillRect(x, y, width, height); g.drawString("Pressed Here!", x, y); } else if(flag == 5) { g.drawString("Mouse Entered Applet!", 5, 80); g.fillRect(x, y, width, height); g.drawString("Mouse Released!", x, y); } } 7. Listener interface method, called when the mouse enters the applet. public void mouseEntered(MouseEvent me) { flag = 1; repaint(); } 8. Listener interface method, called when the mouse exits the applet. public void mouseExited(MouseEvent me) { flag = 2; repaint(); } // 9. Listener interface method, called when the mouse button is clicked. public void mouseClicked(MouseEvent me) { flag = 3; x = me.getX(); y = me.getY(); repaint(); } // 10. Listener interface method, called when the mouse button is pressed. public void mousePressed(MouseEvent me) { flag = 4; x = me.getX(); y = me.getY(); repaint(); } // 11. Listener interface method, called when the mouse button is released. public void mouseReleased(MouseEvent me) { flag = 5; x = me.getX(); y = me.getY(); repaint(); } } Listing 3.3 Mouse Motion Events (TMouseMotion.java) // Demonstrates mouse motion events. /* * * */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TMouseMotion extends JApplet { int x, y; int flag; public void init() { // 1. Create an object of the custom listener and register it with // the applet. CustomListener ct = new CustomListener(this); this.addMouseMotionListener(ct); } // 2. The update() method to repair the applet's background. // Warning: This can reduce the performance and lead to flickering. // The workaround is to use a JPanel attached to the applet. public void update(Graphics g) { g.setColor(this.getBackground()); g.fillRect(0, 0, getWidth(), getHeight()); paint(g); } // 3. The paint() method to display the mode of mouse motion // and its location, depending on the flag value. public void paint(Graphics g) { g.setColor(Color.blue); g.drawString("Drag/Move the Mouse ...", 5, 20); g.setColor(Color.red); if(flag == 1) { g.drawString("Don't Move! Drag the Mouse!", 5, 85); g.drawString("Cursor Coordinates: " + x + ", " + y, 5, 95); } else if(flag == 2) { g.drawString("Don't Drag! Move the Mouse!", 5, 85); g.drawString("Cursor Coordinates: " + x + ", " + y, 5, 95); } } } // 4. The mouse motion listener. class CustomListener implements MouseMotionListener { TMouseMotion tm; public CustomListener(TMouseMotion tm) { this.tm = tm; } // Executed when the mouse is moved. public void mouseMoved(MouseEvent me) { // Set the flag value tm.flag = 1; // Get the coordinates of the mouse pointer. tm.x = me.getX(); tm.y = me.getY(); // Repaint the applet. tm.repaint(); } // Executed when the mouse is dragged. public void mouseDragged(MouseEvent me) { // Set the flag value tm.flag = 2; // Get the coordinates of the mouse pointer. tm.x = me.getX(); tm.y = me.getY(); // Repaint the applet. tm.repaint(); } } Listing 3.4 Adapters for Event Handling (TKeyEvent.java) // Demonstrates adapters and key events. /* * * */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class TKeyEvent extends JApplet { Container contentPane; JLabel label; TextField textField; public void init() { // 1. Get the handle on the applet's content pane. contentPane = this.getContentPane(); // 2. Create a text field and add a key listener to it. textField = new TextField(25); // of 25 char width textField.addKeyListener(new MyKeyListener()); // 3. Create a button object and register an action // listener. Button button = new Button("Clear"); button.addActionListener(new ButtonListener()); // 4. Create a label with the titled border. label = new JLabel("Key Typed: Nill"); label.setBorder(BorderFactory.createTitledBorder( "You Pressed the Following Key")); // 5. Add the text field and button to the applet's // content pane. contentPane.setLayout(new BorderLayout()); contentPane.add("North", textField); contentPane.add(label); contentPane.add("South", button); // 6. Get the focus on to the text field. // Note: You can do this only after you add // the text field to the container. textField.requestFocus(); } // 7. Create the key listener class. class MyKeyListener extends KeyAdapter { public void keyTyped(KeyEvent e) { char c = e.getKeyChar(); label.setText ("Key Typed: " + c); } } // 8. Create the button listener class. class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { //Reset the text components. textField.setText(""); //Return the focus to the text field. textField.requestFocus(); } } }