Event Processing - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Event Processing

Description:

Event Processing. Graphical User Interfaces and Events. Event Handler Methods. Text field ... Component Mouse Mouse pressed or released, mouse moved into or ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 18
Provided by: gaylejya
Category:
Tags: event | processing

less

Transcript and Presenter's Notes

Title: Event Processing


1
Event Processing
  • Graphical User Interfaces and Events

2
OS Interrupt
Generates an Event
Object e.g. button
Listener or middleman (interface)
Event Handler Methods
3
(No Transcript)
4
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

5
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

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
7
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.addActionListe
ner(new ColorListener()) _button2 new
CreateButton (Color.blue, "2", this)
..
8
Event 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
9
public 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
10
Test Program!This exercise simply extends
previous lab-place in same dropbox.
11
To Follow Along and Prepare for Lab 10
  • Download holder package from my Web space
  • http//www.personal.psu.edu/gjy1/infsy535/holder/

12
Radio 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

14
import javax.swing.ButtonGroup
private JButtonGroup _buttonGroup
_buttonGroup new JButtonGroup ()
15
Add 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!

17
Lab 9
  • Download holder directory from
  • http//www.personal.psu.edu/gjy1/infsy535/hold
    er/
Write a Comment
User Comments (0)
About PowerShow.com