GUI Event Model: - Operating System (Windows, JVM) Runs The
GUI Event Model: - Operating System (Windows, JVM) Runs The
GUI Event Model: - Operating System (Windows, JVM) Runs The
00 Lecture 16
Event Delegation
Button Example
import java.awt.*; import javax.swing.*;
import java.awt.event.*;
private int i= 0;
}
7
countButton.addActionListener(this)
Event Listeners
ComboBoxApp
public class ComboBoxApp
extends JFrame {
public ComboBoxApp() {
setTitle("ComboBox Example");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(XSIZE, YSIZE);
10
ComboBoxApp, 2
class ComboPanel extends JPanel implements ActionListener
{
public ComboPanel() {
String[][] fontOptions= {
{"Monospaced", "Serif", "SansSerif"},
{"PLAIN", "BOLD", "ITALIC"},
{"10", "12", "14", "18", "24", "36"} };
showFont.setHorizontalAlignment( SwingConstants.CENTER );
curSize));
showFont.setText(curFamily+" "+curStyle+" "+curSize);
11
ComboBoxApp, 3
JPanel comboPanel = new JPanel();
comboPanel.add(chFamily);
comboPanel.add(chStyle);
comboPanel.add(chSize);
add( comboPanel, "North" );
add( showFont, "Center" );
chFamily.addActionListener(this);
chStyle.addActionListener(this);
chSize.addActionListener(this);
}
ComboBoxApp, 4
public void actionPerformed(ActionEvent e) {
if (e.getSource() == chFamily)
curFamily= (String) chFamily.getSelectedItem();
else if (e.getSource() == chStyle)
curStyle= (String) chStyle.getSelectedItem();
else if (e.getSource() == chSize)
curSize= Integer.parseInt(
(String)chSize.getSelectedItem());
showFont.setFont( new Font( curFamily,
styleIndex(curStyle), curSize) );
showFont.setText(curFamily +" "+ curStyle +" "+ curSize);
}
Event Types
14
Mousing Example
A. Download Mousing.java to a new directory:
– In your web browser go to: Mousing.java
15
Mousing Example, 2
C. You might wonder how to find out what events a component can
produce. Look at the Javadoc for JButton and search for
addEventTypeListener methods. When you find one (and check
base classes also), follow the hyperlink to the event listener
interface. That will list a set of event types and callbacks that the
component can generate. Once you have found all the
add...Listener methods, you will have found all the events.
16
AnonComboBoxApp
class AnonComboPanel
extends JPanel //implements ActionListener {
. . .
//chFamily.addActionListener(this);
chFamily.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
curFamily= (String) chFamily.getSelectedItem();
showFont.setFont( new Font( curFamily,
styleIndex(curStyle), curSize) );
showFont.setText(curFamily + " " + curStyle +
" " + curSize);
}
} );
//chStyle/chSize.addActionListener(this);
//no joint actionPerformed() method
18
AnonComboBoxApp, 2
chFamily.addActionListener(
new ActionListener() { . . . }
);
19
AnonComboBoxApp, 3
20
10
Clock Example
21
Clock Example
C. (Cont’d)
– paintComponent(Graphics g): this method draws the clock
and the hours and minutes hands according to the minutes
value (Once again, don’t worry about the details of how this is
done. We'll explore this kind of drawing in the next class.)
– tick(): this method increments minutes by one then
repaints the clock
• repaint() will call the paintComponent method which will redraw
the clock with the clock hands adjusted to the new minutes
value)
– reset(): This method resets minutes to zero then repaints
the clock.
– getMinutes(): Access method that returns minutes.
22
11
Clock Example
C. (cont’d) To sum up all you need to know about this class (and this
is the beauty of OOP) is the behavior dictated by its public
methods, that is tick(), getMinutes() and reset() methods,
without having to worry about how the clock will draw itself.
E. Compile and run your project. You can see the GUI, but so far the
program doesn’t do anything. In this exercise, we’re going to
construct the GUI event model for this clock.
23
12
25
26
13
B. Call this method from the appropriate location. Compile and run.
27
Java® is a trademark or registered trademark of Sun Microsystems, Inc. in the United States and
other countries. 14