230 likes | 312 Views
Event Driven Programming. 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?
E N D
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? • Are we there yet? • Are we there yet? • Are we there yet? • Are we there yet?
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 fast-efficient 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 pre-programmed in to the system. These include buttons, text areas, etc.
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.
We want a small window that will display Two editable numbers One non-editable sum An ADD Button A Clear Button Adding Machine
How it works Unlike the other programs you have written event-driven programming doesn’t follow a single path The functionality of the text windows is built-in Different objects will be constructed to handle the action when each button is pressed
The Code import java.awt.event.*; import javax.swing.*; import java.awt.*; class Adder extends WindowAdapter { // The widget references public JFrame f; public JPanel p; public JTextField top; public JTextField bottom; public JTextField sum; public JButton add; public JButton clear;
The Code // Constructor public Adder() { // Top level window on screen f = new JFrame("Adding Machine"); f.setSize(200, 200); // Panel to hold components p = new JPanel(); // 3 text fields to hold 2 numbers and sum top = new JTextField("0.0", 20); top.setHorizontalAlignment(JTextField.RIGHT); bottom = new JTextField("0.0", 20);
The Code // Still in constructor bottom.setHorizontalAlignment (JTextField.RIGHT); sum = new JTextField("0.0", 20); sum.setHorizontalAlignment(JTextField.RIGHT); sum.setEditable(false); // Add and clear buttons add = new JButton("ADD"); add.addActionListener(new AddButtonHandler(top, bottom, sum)); clear = new JButton("CLEAR"); clear.addActionListener(new ClearButtonHandler(top, bottom, sum));
The Code // Still in constructor // How it looks p.setLayout (new BoxLayout(p, BoxLayout.Y_AXIS)); f.getContentPane().add(p); p.add(top); p.add(bottom); p.add(sum); p.add(add); p.add(clear); f.addWindowListener(this); f.show(); }
The Code // Handle clicking on window close X public void windowClosing(WindowEvent 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.*; class ClearButtonHandler implements ActionListener { // Widgets JTextField top, bottom, sum; // Constructor public ClearButtonHandler(JTextField top, JTextField bottom, JTextField sum) { this.top = top; this.bottom = bottom; this.sum = sum; }
Event Handlers import java.awt.event.*; import javax.swing.*; import java.awt.*; // Clear all the text fields public void actionPerformed(ActionEvent e) { top.setText("0.0"); bottom.setText("0.0"); sum.setText("0.0"); } }
Event Handlers import java.awt.event.*; import javax.swing.*; import java.awt.*; class AddButtonHandler implements ActionListener { // Widget References JTextField top, bottom, sum; // Constructor public AddButtonHandler(JTextField top, JTextField bottom, JTextField sum) { this.top = top; this.bottom = bottom; this.sum = sum; }
Event Handlers // Get text field contents, convert, add and disp public void actionPerformed(ActionEvent e) { double t, b, total; String tString = top.getText(); String bString = bottom.getText(); try { t = Double.parseDouble(tString); } catch(Exception e1) { t = 0.0; top.setText("Error!"); }
Event Handlers try { b = Double.parseDouble(bString); } catch(Exception e2) { b = 0.0; bottom.setText("Error!"); } total = t + b; sum.setText((new Double(total)).toString()); } }