Events and Event Processing(Powerpoint)
Download
Report
Transcript Events and Event Processing(Powerpoint)
Event Processing
Graphical User Interfaces
and Events
OS Interrupt
Generates an Event
Screen
Press Button
(Buttons other objects
generate events: Source)
JAVA AWT
Environment:
Messages are sent
between JAVA
Objects
Object
e.g.
button
Listener or
middleman
Event Notification (interface)
Text field
Object
Responder
Event Handler Methods
Class Interface
Event Handler Methods
Generated by
ActionEvent
actionPerformed
Button pressed
JTextField-text changed
JComboBox-alternative
chosen
ChangeEvent
stateChanged
JComponent-state
changed
FocusEvent
focusGained
focusLost
Component-gained or
lost focus
WindowEvent
MouseEvent
Window opened, closed,
is closing, is iconified, or
restored
mouseClicked
mouseEntered
mouseExited
mousePressed
mouseReleased
mouseDragged
mouseMoved
Component Mouse
Mouse pressed or
released, mouse moved
into or out of component
Component moved or
dragged
Event Handlers
(ColorButton.java)
Declare an event handler class:
•
•
Specify a class that implements a listener interface
or
Extends a class that implements a listener interface
A line of code “registers” an instance of the
event
In the event handler class, code the listener
class using the event method
Listener classes
The listener class
private class ColorListener
implements java.awt.event.ActionListener
listener class methods must
be defined in the listener interface
implemented in our programs
Always receives an object
private class ColorListener implements
java.awt.event.ActionListener
{
public void actionPerformed
(java.awt.event.ActionEvent e)
{
Listener interface
Event method
<you will add code to tell system what should
happen when event is triggered>
}
}
Note: Nested within the ButtonPanelGrid or the
CreateButton class
Event Handling
Note that the similarity of the buttons!
public class ButtonPanelGrid extends JPanel
{
JButton _button1,_button2,_button3, _button4……….;
public ButtonPanelGrid () {
super ();
this.setLayout(new GridLayout (4,2));
_button1 = new CreateButton (Color.red, "1", this);
_button1.addActionListener(new ColorListener());
_button2 = new CreateButton (Color.blue, "2", this);
………………………………..
}
Event Handling
Alternative: Change your code!
public class ButtonPanelGrid extends JPanel
{
public ButtonPanelGrid (ColorHolder _holder)
{
Add code to receive the ColorHolder object
super ();
this.setLayout(new GridLayout (4,2));
new CreateButton (Color.red, "1", this, _holder);
new CreateButton (Color.blue, "2", this, _holder);
<create other buttons in same manner>
}
}
Add code to pass the ColorHolder object
public class CreateButton extends JButton {
private Colorable _colorable;
private JButton _button;
public CreateButton (Color aColor, String message,JPanel _p, Colorable h) {
super (message);
this.setBackground(aColor);
this.addActionListener(new ColorListener());
_p.add(this);
_colorable = h;
Registers the event
_button = this; }
Event handler class
private class ColorListener implements
implements the
java.awt.event.ActionListener { interface
public void actionPerformed (java.awt.event.ActionEvent e) {
Color _c;
_c=_colorable.getColor (_button);
Event handler;
_colorable.setColor (_c); }
}
}
code the listener
Test Program!
This exercise simply
extends previous labplace in same dropbox.
To Follow Along and Prepare
for Lab 10
Download holder package from my Web
space:
http://www.personal.psu.edu/gjy1/infsy535/holder/
Radio Buttons
Mutually exclusive buttons
Has meaning only if there are more
than one button
When one is “clicked,” others are
disabled automatically
Radio Buttons work in a set
ButtonGroup class is used to
define a set of radio buttons
import javax.swing.ButtonGroup;
private JButtonGroup _buttonGroup ;
_buttonGroup = new JButtonGroup ();
ButtonGroup _buttonGroup;
_buttonGroup = new ButtonGroup ();
_blueButton = new ColorButton
(java.awt.Color.blue, aColorable),
_buttonGroup, true);;
group.add (_blueButton);
Note: ColorButton
Creates a radiobutton
Note: ColorHolder Interface is passed to
both panels, etc.
Means by which the button color is stored
and subsequently retrieved in the
ColorShapes panel!
Lab 9
Download holder directory from:
http://www.personal.psu.edu/gjy1/infsy535/h
older/