1 / 18

Event Driven Programming

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?

davisnathan
Download Presentation

Event Driven Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Event Driven Programming

  2. 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?

  3. 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.

  4. 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.

  5. 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.

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

  7. 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

  8. 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;

  9. 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);

  10. 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));

  11. 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(); }

  12. 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(); } }

  13. 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; }

  14. 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"); } }

  15. 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; }

  16. 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!"); }

  17. 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()); } }

More Related