













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The concept of event handling in java, with a focus on listening to events such as mouse clicks, mouse movements, and keystrokes. It covers the use of inner and anonymous classes, and the implementation of listener methods. Examples are provided to illustrate the concepts.
Typology: Lecture notes
1 / 21
This page cannot be seen from the preview
Don't miss anything!














Why men think “computer” should be a feminine word
Why women think “computer” should be a masculine word
Prelim: Thursday, 7 March: 7:30–9:00. Place: TBA Makeup for those with approved conflicts: 5:30-7:00. Place: TBA Can’t make either? Contact Prof. Birman. Review sessions: Sunday, 3 March: 1-3PM and 3-5PM, Olin 155
Summary of what exam covers: Basically, everything before GUIs (i.e. before this week) More detail will be provided in a document to be this evening on the course website and Piazza.
Previous exams: On course website (click “Exams”)
Note: Today’s slides have been updated since posting on course website. Today’s version will be put on website this afternoon.
A1. 164 graded. 40 got 100. mean 92.3, median 94, stdev. 7.
jb1= new JButton() // jb1 has no text on it jb2= new JButton(“first”) // jb2 has label “first” on it
jb2.isEnabled() // true iff a click on button can be // detected jb2.setEnabled(b); // Set enabled property
jb2.addActionListener(object); // object must have a method, // which is called when button jb2 clicked (next page)
At least 100 more methods; these are most important
JButton is in package javax.swing
A JPanel that is painted
these are different kinds of events, and they need different listener methods
Class Square
/** Instance: JPanel of size (WIDTH, HEIGHT). Green or red: / public class Square extends JPanel { public static final int HEIGHT= 70; public static final int WIDTH= 70; private int x, y; // Panel is at (x, y) private boolean hasDisk= false ; /* Const: square at (x, y). Red/green? Parity of x+y. / public Square( int x, int y) { this .x= x; this .y= y; setPreferredSize( new Dimension(WIDTH,HEIGHT)); } /* Complement the "has pink disk" property */ public void complementDisk() { hasDisk=! hasDisk; repaint(); // Ask the system to repaint the square }
continued on later
Class Square
/** Remove pink disk (if present) */ public void clearDisk() { hasDisk= false ; // Ask system to // repaint square repaint(); }
continuation of class Square
/* paint this square using g. System calls paint whenever square has to be redrawn.*/ public void paint(Graphics g) { if ((x+y)%2 == 0) g.setColor(Color.green); else g.setColor(Color.red); g.fillRect(0, 0, WIDTH-1, HEIGHT-1); if (hasDisk) { g.setColor(Color.pink); g.fillOval(7, 7, WIDTH-14, HEIGHT-14); } g.setColor(Color.black); g.drawRect(0, 0, WIDTH-1,HEIGHT-1); g.drawString("("+x+", "+y+")", 10, 5+HEIGHT/2); } }
void mouseClicked(MouseEvent e); void mouseEntered(MouseEvent e); void mouseExited(MouseEvent e); void mousePressed(MouseEvent e); void mouseReleased(MouseEvent e); }
In package java.awt.event
Having to write all of these in a class that implements MouseListener, even though you don’t want to use all of them, can be a pain. So, a class is provided that implements them in a painless.
Javax.swing.event.MouseInputAdapter implements MouseListener
DemoMouseEvents() { …
… }
DemoMouseEvents
a
JFrame
dma a1 lab1 … lab1 …
lab1.addMouseListener(dma);
mouseClicked() mouseEntered() mouseExited() mousePressed() mouseReleased()
a
mouseClicked() {
…
}
MouseEvents
MouseListener
A class that listens to a mouseclick in a Square
import javax.swing.; import javax.swing.event.; import java.awt.; import java.awt.event.;
/** Contains a method that responds to a mouse click in a Square */ public class MouseEvents extends MouseInputAdapter { // Complement "has pink disk" property public void mouseClicked(MouseEvent e) { Object ob= e.getSource(); if (ob instanceof Square) { ((Square)ob).complementDisk(); } } }
This class has several methods (that do nothing) that process mouse events: mouse click mouse press mouse release mouse enters component mouse leaves component mouse dragged beginning in component Our class overrides only the method that processes mouse clicks
red: listening blue: placing
Listening to the keyboard
import java.awt.; import java.awt.event.; import javax.swing.*;
public class AllCaps extends KeyAdapter { JFrame capsFrame= new JFrame(); JLabel capsLabel= new JLabel(); public AllCaps() { capsLabel.setHorizontalAlignment(SwingConstants.CENTER); capsLabel.setText(":)"); capsFrame.setSize(200,200); Container c= capsFrame.getContentPane(); c.add(capsLabel); capsFrame.addKeyListener( this ); capsFrame.show(); } public void keyPressed (KeyEvent e) { char typedChar= e.getKeyChar(); capsLabel.setText(("'" + typedChar + "'").toUpperCase()); } }
red: listening blue: placing
public class BDemo3 extends JFrame implements ActionListener { private JButton wButt, eButt …; public ButtonDemo3() { Add buttons to content pane, enable ne, disable the other wButt.addActionListener( this ); eButt.addActionListener( new BeListener()); } public void actionPerformed(ActionEvent e) { boolean b= eButt.isEnabled(); eButt.setEnabled(!b); wButt.setEnabled(b); } }
class BeListener implements ActionListener { public void actionPerformed(ActionEvent e) { boolean b= eButt.isEnabled(); eButt.setEnabled(!b); wButt.setEnabled(b); }
A listener for eastButt
Doesn’t work! Can’t reference eButt, wButt
Have a different listener for each button
Problem: can’t give a function as a parameter: public void m() { … eButt.addActionListener(aP); } public void aP(ActionEvent e) { body }
Why not just give eButt the function to call? Can’t do it in Java! Can in some other languages
public void m() { … eButt.addActionListener( new C()); }
public class C implements IN { public void aP(ActionEvent e) { body } }
Java says: provide class C that wraps method; give eButt an object of class C
C must implement interface IN that has abstract method aP
Have a class for which only one object is created? Use an anonymous class. Use sparingly, and only when the anonymous class has 1 or 2 methods in it, because the syntax is ugly, complex, hard to understand.
public class BDemo3 extends JFrame implements ActionListener { private JButton wButt, eButt …; public ButtonDemo3() { … eButt.addActionListener( new BeListener()); } public void actionPerformed(ActionEvent e) { … } private class BeListener implements ActionListener { public void actionPerformed(ActionEvent e) { body } } } 1 object of BeListener created. Ripe for making anonymous