Event Handling
Event handling is fundamental to Java programming Writing event driven program is a two step ... The class AWTEvent defined within the java.awt.
Concepts of Programming Languages - Lecture 20 - Event-Driven
Definition. The event handler is a segment of code that is executed in response to an event. Patrick Donnelly (Montana State University).
GUI Event-Driven Programming
Registering listeners to handle events Proper interaction between UI and program threads ... nested class: A class defined inside of another class.
Event Handling in Prolog
predicates are defined using clauses and literals and so any failure or message If a Prolog program has to respond to external events it has to provide.
Event Handling in Prolog
predicates are defined using clauses and literals and so any failure or message If a Prolog program has to respond to external events it has to provide.
5 Interactive Programs: Events and Event-Handling
the event happens. This means we need to write methods that describe the actions objects in the 5-1 Interactive Programming: Events and Event-Handlers.
Event Handling in JavaFX
Event Driven Programming JavaFX looks for a registered "Event Listener" and calls it ... Define an (inner) class that implements EventHandler.
Event Driven Programming for Embedded Systems - A Finite State
sm define handle event(fsm name) - This is not an interface for the programmer. This macro defines the state machine handling function for the events. Note: It
CAPL Scripting Quickstart
CAPL (Communication Access Programming Language) For CANalyzer and CANoe. CAPL Scripting Quickstart Multiple pre-defined event handlers exist for.
A New Approach to Event- Driven Programming
In order to design a well structured program event handling and corresponding part is defined as a declarative specification of the event-handling in a.
Concepts of Programming Languages
Lecture 20 - Event-Driven Programming
Patrick Donnelly
Montana State University
Spring 2014
Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 1 / 37Administrivia
Assignments:
Programming #4 : due 04.28
Reading:
Chapter 14
Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 2 / 37 Of all men"s miseries the bitterest is this, to know so much and to have control over nothing.Herodotus (484-432 BC)
Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 3 / 37Event-Driven Programming
A conventional model of computation has the program prescribe the exact order of input.Programs terminate once the input is exhausted.
Event-driven programs do not control the sequence in which input events occur. Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 4 / 37Event Handling
Definition
Aneventis a notification that something specific has occurred, such as a mouse click on a graphical button.Definition Theevent handleris a segment of code that is executed in response to an event. Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 5 / 37Examples
GUI applications: Model-View-Controller design
Embedded applications:cell phones
car engines airplanes Computation as interaction [Stein, 1998]:Computation is a community of persistent entities coupled together by their ongoing interactive behavior ... Beginning and end, when present, are special cases that can often be ignored. Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 6 / 37Imperative and Event-Driven Paradigms Contrasted
Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 7 / 37Event Sources
Input to an event-driven program comes from autonomous event sources.Events occur asynchronously.
Example:human,
robot sensors, engine sensors Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 8 / 37Event Properties
1An event-driven program has no perceived stopping point.
2The traditional read-eval-print loop does not explicitly appear.
3An application processes an input and exits.
Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 9 / 37Model-View-Controller (MVC)
Model: the object being implemented. Ex: game, calculator. Controller: input mechanisms. Ex: buttons, menus, combo boxes.View: output.
Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 10 / 37Ex: Tic-Tac-Toe Model
Whose turn is it?
State of the board.
Has someone won?
Are there no empty squares?
Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 11 / 37Java GUI Application
Definition
AGUIapplication is a program that runs in its own window andcommunicates with users using buttons, menus, mouse clicks, etc.A GUI application often has a paint method, which is invoked
whenever the application needs to repaint itself. Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 12 / 37Java Swing GUI Components
Text box is an object of classJTextField
Radio button is an object of classJRadioButton
Applet"s display is a frame, a multilayered structure Content pane is one layer, where applets put outputGUI components can be placed in a frame
Layout manager objects are used to control the placement of components Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 13 / 37The Java Event Model
Definition
User interactions with GUI components create events that can becaught by event handlers, calledevent listeners.An event generator tells a listener of an event by sending a message
An interface is used to make event-handling methods conform to a standard protocol A class that implements a listener must implement an interface for the listener Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 14 / 37Events in Java
Subclasses ofAWTEvent
Event sources in Swing are subclasses ofJComponentProgram must listen for eventsExample
for aJButtonb: b.addActionListener(listener) Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 15 / 37Java Class AWTEvent and Its Subclasses
Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 16 / 37 Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 17 / 37 Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 18 / 37The Java Event Model
One class of events isItemEvent, which is associated with the event of clicking a checkbox, a radio button, or a list itemTheItemListener interface prescribes a method,
itemStateChanged, which is a handler forItemEventeventsThe listener is created withaddItemListenerPatrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 19 / 37
Components and their Event Handlers
Widget Listener InterfaceJButton ActionListener actionPerformed(ActionEvent e) JComboBox ActionListener actionPerformed(ActionEvent e)JLabel MouseListener mouseClicked(MouseEvent e)
mouseEntered(MouseEvent e) mouseExited(MouseEvent e) mousePressed(MouseEvent e) mouseReleased(MouseEvent e)MouseMotionListener mouseDragged(MouseEvent e)
mouseMoved(MouseEvent e) JTextArea ActionListener actionPerformed(ActionEvent e) JTextField ActionListener actionPerformed(ActionEvent e) Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 20 / 37GUI Example
import javax.swing.JFrame; public classGUIApp {
public static void main (String[ ] args) {JFrame frame =
newJFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyApp app =
newMyApp( );
JPanel
frame.getContentPane().add(app); frame.show( ); Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 21 / 37GUI Example using MVC
combo: User selects Nothing, Rectangle, Message echoArea: Report eventstyping: Enter user messagesPatrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 22 / 37
GUI Design
Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 23 / 37Instance Variables
first click s x y coordinates private int lastX = 0; private int lastY = 0; private int clickNumber = 0; privateJComboBox combo;
privateString[ ] choices =
Nothing
Rectangle
Message
privateJTextArea echoArea;
privateJTextField typing;
Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 24 / 37Initialization Code (1/2)
publicSkeleton( ) {
Set the background color and mouse listener setBackground(Color.white); addMouseListener( newMouseHandler());
Add a button to the PanelJButton clearButton =
newJButton(
Clear clearButton.setForeground(Color.black); add(clearButton); clearButton.addActionListener( newClearButtonHandler());
Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 25 / 37Initialization Code (2/2)
Create
a menu of user combos and add it combo = newJComboBox(choices);
add(combo); combo.addActionListener( newComboHandler());
Add aTextField
and aTextArea
typing = newJTextField(20);
add(typing); typing.addActionListener( newTextHandler());
echoArea = newJTextArea(2, 40);
echoArea.setEditable( false add(echoArea); Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 26 / 37Action Listener
private classComboHandler
implementsActionListener {
public void actionPerformed (ActionEvent e) {String c = (String)
(combo.getSelectedItem()); echoArea.setText( Combo selected + c); clickNumber = 0; if (c.equals(Rectangle
echoArea.append( nClick to set upper left corner of the rectangle else if (c.equals(Message
echoArea.append( nEnter message in the text area Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 27 / 37The User Selects Rectangle from the Menu
Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 28 / 37 mouseClickedHandler (1/2) private classMouseHandler
extendsMouseAdapter {
public void mouseClicked(MouseEvent e) { int x = e.getX(); int y = e.getY(); echoArea.setText( MouseClicked
at e.getX() + + e.getY() + nGraphics g = getGraphics();
if (combo.getSelectedItem(). equals(Rectangle
clickNumber = clickNumber + 1; Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 29 / 37 mouseClickedHandler (2/2) is it the first click if (clickNumber % 2 == 1) { echoArea.append( Click to set lower right corner of the rectangle lastX = x; lastY = y; or the second else g.drawRect(lastX, lastY,Math.abs(x-lastX), Math.abs(y-lastY));
else if (combo.getSelectedItem().equals(Message
for a message display it g.drawString(typing.getText(), x, y); mouseClicked Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 30 / 37Selecting Rectangle Choice and ClickingTwice
Patrick Donnelly (Montana State University)Concepts of Programming LanguagesSpring 2014 31 / 37 mainmethod public static void main(String args[]) {JFrame frame =
newquotesdbs_dbs21.pdfusesText_27[PDF] event handling in java using applet
[PDF] event handling javascript definition
[PDF] event handling simple definition
[PDF] event handling using applet
[PDF] event listener in java applet
[PDF] event management logistics planning guide
[PDF] event management plan checklist and guide
[PDF] event marketing pdf
[PDF] event marketing plan example
[PDF] event marketing plan example pdf
[PDF] event marketing plan template pdf
[PDF] event marketing plan template ppt
[PDF] event marketing plan template word
[PDF] event marketing process