Event Driven Programming and Graphical User Interface - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Event Driven Programming and Graphical User Interface

Description:

Control-driven: The order in the program code determines the sequence of events. ... to specific components and define action to be performed in actionPerformed ... – PowerPoint PPT presentation

Number of Views:77
Avg rating:3.0/5.0
Slides: 13
Provided by: kumarm8
Category:

less

Transcript and Presenter's Notes

Title: Event Driven Programming and Graphical User Interface


1
Event Driven Programming and Graphical User
Interface
  • B.Ramamurthy

2
Types of Programs
  • Control-driven The order in the program code
    determines the sequence of events.
  • The actions are predetermined.
  • Event-driven The operation of the program
    depends on what you do with the controls
    presented (usually by the GUI)
  • Selecting menu items, pressing buttons, dialog
    interaction cause actions within the program.

3
Events
  • 1. Actions such as clicking a button, moving a
    mouse, are recognized and identified by the
    operating systems(OS) or JVM.
  • 2. For each action, OS/JVM determines which of
    the many currently running programs should
    receive the signal (of the action)
  • 3. The signals that the application receives from
    the OS/JVM as result of the actions are called
    events.

4
Event Generation
User Interface
Mouse actions, Keystrokes
Operating Systems
JAVA API /Windows
Events
Methods and handlers
Methods and handlers
Application 1
Application 2
5
Event Handlers
  • An application responds to the events by
    executing particular code meant for each type of
    event.
  • Not all events need to be handled by an
    application. For example, a drawing application
    may be interested in handling only mouse
    movements.
  • As a designer of an event-driven application you
    will write classes/methods to handle the relevant
    events.

6
Event Handling Process
  • Source of an event is modeled as an object. Ex
    button clicks object is a button
  • Type of the event ActionEvent, WindowEvent,
    MouseEvent etc. Ex An ActionEvent object is
    passed to the application that contains
    information about the action.
  • Target of an event Listener object of the event.
    Passing the event to a listener results in
    calling a particular method of the listener
    object.

7
JDK1.2 Additions
JFC
java2D(3D)
AWT
DragDrop
Swing
Accessibility
(Speech, Sight API)
Collection classes
Bug fixes
JDK1.2 changes
8
Swing Component
  • Two distinguishing characteristics are
  • 1. It supports pluggable Look and Feel. Besides
    run-anywhere, appearance of a component may be
    changed at run-time to look and feel like the
    system on which it is running.
  • 2. Swing components are Lightweight components
    they do not dependent on any native system
    classes.

9
Swing Components (controls)
  • JApplet
  • JButton
  • JCheckBox
  • JTable
  • JTextPane
  • JSlider
  • JTree
  • JPopupMenu
  • JToolTip
  • JScrollPane
  • JDialog
  • JMenuBar
  • etc.
  • Demo jdk1.2/demo/jfc/SwingSet/SwingSetApplet.java
  • Note J prefix to distinguish it from regular
    AWT components.

10
Steps in GUI Application Development
  • Extend the application class from JFrame
  • Declare all the components needed.
  • Define the constructor.
  • Instantiate a ContentPane and Define layout.
  • Instantiate objects for the components and set
    their values.
  • Add them to the ContentPane object.
  • Add any listeners/handlers to specific components
    and define action to be performed in
    actionPerformed method.
  • Add a main method that instantiate the object
    created above and takes care of Window closing.

11
Event Handling
  • Three ways
  • Frame itself is a (mouse) listener, with an
    action performed method.
  • Event handling delegated to an object specially
    instantiated for this purpose.
  • Anonymous class/object to handle just one event.
  • Note last two methods use inner class. We will
    examine the last two ways in the next example.

12
Example
op1
op2
result
Add
Sub
This is a representation of the GUI for Lab2.
Lab2 illustrates all the concepts discussed
above.
Write a Comment
User Comments (0)
About PowerShow.com