Title: Event Processing
1Event Processing
- Graphical User Interfaces and Events
2OS Interrupt
Generates an Event
Object e.g. button
Listener or middleman (interface)
Event Handler Methods
3(No Transcript)
4Event 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
5Listener 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
6 private class ColorListener implements
java.awt.event.ActionListener public void
actionPerformed
(java.awt.event.ActionEvent e) ltyou will add
code to tell system what should
happen when event is triggeredgt
Listener interface
Event method
Note Nested within the ButtonPanelGrid or the
CreateButton class
7Event 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.addActionListe
ner(new ColorListener()) _button2 new
CreateButton (Color.blue, "2", this)
..
8Event Handling
Alternative Change your code! public class
ButtonPanelGrid extends JPanel public
ButtonPanelGrid (ColorHolder _holder)
super () this.setLayout(new
GridLayout (4,2)) new CreateButton (Color.red,
"1", this, _holder) new
CreateButton (Color.blue, "2", this, _holder)
ltcreate other buttons in
same mannergt
Add code to receive the ColorHolder object
Add code to pass the ColorHolder object
9public class CreateButton extends JButton
private Colorable _colorable
private JButton _button public
CreateButton (Color aColor, String message,JPanel
_p, Colorable h) super (message) this.setBa
ckground(aColor) this.addActionListener(new
ColorListener()) _p.add(this) _colorable
h _button this private class
ColorListener implements
java.awt.event.ActionListener
public void actionPerformed
(java.awt.event.ActionEvent e) Color
_c _c_colorable.getColor (_button) _colorab
le.setColor (_c)
Registers the event
Event handler class implements the interface
Event handler code the listener
10Test Program!This exercise simply extends
previous lab-place in same dropbox.
11To Follow Along and Prepare for Lab 10
- Download holder package from my Web space
- http//www.personal.psu.edu/gjy1/infsy535/holder/
12Radio Buttons
- Mutually exclusive buttons
- Has meaning only if there are more
- than one button
- When one is clicked, others are
- disabled automatically
13- Radio Buttons work in a set
- ButtonGroup class is used to
- define a set of radio buttons
14import javax.swing.ButtonGroup
private JButtonGroup _buttonGroup
_buttonGroup new JButtonGroup ()
15Add Radio Buttons
ButtonGroup _buttonGroup _buttonGroup new
ButtonGroup () _blueButton new ColorButton
(java.awt.Color.blue, aColorable),
_buttonGroup, true) group.add (_blueButton)
Note ColorButton Creates a radiobutton
16- Note ColorHolder Interface is passed to both
panels, etc. - Means by which the button color is stored and
subsequently retrieved in the ColorShapes panel! -
17Lab 9
- Download holder directory from
- http//www.personal.psu.edu/gjy1/infsy535/hold
er/ -