Event Driven Programming Eventdriven Programming In the early

  • Slides: 18
Download presentation
Event Driven Programming

Event Driven Programming

Event-driven Programming • In the early days of computing communication with the outside world

Event-driven Programming • In the early days of computing communication with the outside world was accomplished using a technique called Polling. • This is the technique you used with your Mom and Dad when you were little and went on a long trip • Are we there yet? • Are we there yet?

Event-driven Programming • Eventually your Dad said something like, “I’ll tell you when we

Event-driven Programming • Eventually your Dad said something like, “I’ll tell you when we get there, sweetie. ” • In a like manner early programs would check the keyboard to find out if a key had been pressed and then take action. • As the user interface (keyboard, mouse, screen) became more complex this became problematic. • There was simply too much activity to keep track of what was going on efficiently.

Event-driven Programming • So a system was developed where some very fastefficient code is

Event-driven Programming • So a system was developed where some very fastefficient code is included as either part of the operating system or the JVM • In this approach the programmer can say when a certain event occurs a certain piece of code should be executed • This is refined and expanded with modern languages by providing programmers with a Graphical User Interface Toolkit • This means that items (referred to as widgets) are preprogrammed in to the system. These include buttons, text areas, etc.

Event-driven Programming • Programmers now have tools to design screen layouts and make them

Event-driven Programming • Programmers now have tools to design screen layouts and make them respond as desired. • Today we will show you a small demo and the code that runs it.

Adding Machine • We want a small window that will display • Two editable

Adding Machine • We want a small window that will display • Two editable numbers • One non-editable sum • An ADD Button • A Clear Button

How it works Unlike the other programs you have written event-driven programming doesn’t follow

How it works Unlike the other programs you have written event-driven programming doesn’t follow a single path Different objects will be constructed to handle the action when each button is pressed The functionality of the text windows is built-in

The Code import java. awt. event. *; import javax. swing. *; import java. awt.

The Code import java. awt. event. *; import javax. swing. *; import java. awt. *; class Adder extends Window. Adapter { // The public public widget references JFrame f; JPanel p; JText. Field top; JText. Field bottom; JText. Field sum; JButton add; JButton clear;

The Code // Constructor public Adder() { // Top level window on screen f

The Code // Constructor public Adder() { // Top level window on screen f = new JFrame("Adding Machine"); f. set. Size(200, 200); // Panel to hold components p = new JPanel(); // 3 text fields to hold 2 numbers and sum top = new JText. Field("0. 0", 20); top. set. Horizontal. Alignment(JText. Field. RIGHT); bottom = new JText. Field("0. 0", 20);

The Code // Still in constructor bottom. set. Horizontal. Alignment (JText. Field. RIGHT); sum

The Code // Still in constructor bottom. set. Horizontal. Alignment (JText. Field. RIGHT); sum = new JText. Field("0. 0", 20); sum. set. Horizontal. Alignment(JText. Field. RIGHT); sum. set. Editable(false); // Add and clear buttons add = new JButton("ADD"); add. Action. Listener(new Add. Button. Handler(top, bottom, sum)); clear = new JButton("CLEAR"); clear. add. Action. Listener(new Clear. Button. Handler(top, bottom, sum));

The Code // Still in constructor // How it looks p. set. Layout (new

The Code // Still in constructor // How it looks p. set. Layout (new Box. Layout(p, Box. Layout. Y_AXIS)); f. get. Content. Pane(). add(p); p. add(top); p. add(bottom); p. add(sum); p. add(add); p. add(clear); f. add. Window. Listener(this); f. show(); }

The Code // Handle clicking on window close X public void window. Closing(Window. Event

The Code // Handle clicking on window close X public void window. Closing(Window. Event e) { System. exit(0); } // Start it up public static void main(String args[]) { new Adder(); } }

Event Handlers import java. awt. event. *; import javax. swing. *; import java. awt.

Event Handlers import java. awt. event. *; import javax. swing. *; import java. awt. *; class Clear. Button. Handler implements Action. Listener { // Widgets JText. Field top, bottom, sum; // Constructor public Clear. Button. Handler(JText. Field top, JText. Field bottom, JText. Field sum) { this. top = top; this. bottom = bottom; this. sum = sum; }

Event Handlers import java. awt. event. *; import javax. swing. *; import java. awt.

Event Handlers import java. awt. event. *; import javax. swing. *; import java. awt. *; // Clear all the text fields public void action. Performed(Action. Event e) { top. set. Text("0. 0"); bottom. set. Text("0. 0"); sum. set. Text("0. 0"); } }

Event Handlers import java. awt. event. *; import javax. swing. *; import java. awt.

Event Handlers import java. awt. event. *; import javax. swing. *; import java. awt. *; class Add. Button. Handler implements Action. Listener { // Widget References JText. Field top, bottom, sum; // Constructor public Add. Button. Handler(JText. Field top, JText. Field bottom, JText. Field sum) { this. top = top; this. bottom = bottom; this. sum = sum; }

Event Handlers // Get text field contents, convert, add and disp public void action.

Event Handlers // Get text field contents, convert, add and disp public void action. Performed(Action. Event e) { double t, b, total; String t. String = top. get. Text(); String b. String = bottom. get. Text(); try { t = Double. parse. Double(t. String); } catch(Exception e 1) { t = 0. 0; top. set. Text("Error!"); }

Event Handlers try { b = Double. parse. Double(b. String); } catch(Exception e 2)

Event Handlers try { b = Double. parse. Double(b. String); } catch(Exception e 2) { b = 0. 0; bottom. set. Text("Error!"); } total = t + b; sum. set. Text((new Double(total)). to. String()); } }