Download presentation
Presentation is loading. Please wait.
Published byἍβελ Κουβέλης Modified over 6 years ago
1
Event Driven Programming and Graphical User Interface
B.Ramamurthy 11/11/2018 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. 11/11/2018 B.Ramamurthy
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. 11/11/2018 B.Ramamurthy
4
Event Generation User Interface Mouse actions, Keystrokes
Operating Systems JAVA API /Windows Events Methods and handlers Methods and handlers Application 1 Application 2 11/11/2018 B.Ramamurthy
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. 11/11/2018 B.Ramamurthy
6
Event Handling Process
Source of an event is modeled as an object. Ex: button click’s 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. 11/11/2018 B.Ramamurthy
7
JDK1.2 Additions JFC java2D(3D) AWT Drag&Drop Swing Accessibility
(Speech, Sight API) Collection classes Bug fixes JDK1.2 changes 11/11/2018 B.Ramamurthy
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. 11/11/2018 B.Ramamurthy
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. 11/11/2018 B.Ramamurthy
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/11/2018 B.Ramamurthy
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. 11/11/2018 B.Ramamurthy
12
Example op1 op2 result Add Sub
This is a representation of the GUI for Lab2. Lab2 illustrates all the concepts discussed above. 11/11/2018 B.Ramamurthy
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.