Event-Driven Software Paradigm Today Finish Programming Unit: Discuss Graphics In the old days, computers did exactly what the programmer said Once started, it would run automagically until done Then you could look at the output It was great! All that work it did, so you didn t have to Event-driven systems But Events are Important Too Nowadays, interactivity is important Most software does not run autonomously Even though it used to be the norm Instead, it interacts, with people other software When was the last time you ran a program that was not interactive? Events: requests to interact Events come from outside the program Users can click Other programs can make requests Can you think of an example? Interactive systems respond to events Example: A Java window that sits there doing nothing until you click, type, etc. Have you seen one like that? Java is Event Oriented Java GUIs are normally event driven A Java GUI waits for an event to occur The event makes an event handler run The event handler does something Your program does not call the event handler! So how does the event handler get called? Who calls the event handler? There is a program called java.exe It sets up a JRE (means?) The computer knows when you click The JRE talks with the computer and finds out that you clicked (typed,) JRE then bundles info into an object calls an event handler gives the event handler the object 1
Example: CharCheck //(D. Berleant) import java.lang.*; import java.io.*; import java.awt.*; //swing only //obsoletes some of this import java.awt.event.*; //swing only obsoletes //some of above line import javax.swing.*; public class CharCheck{ static Frame f; //swing version: JFrame f; public static void main (String args[ ]){ DoSomething d=new DoSomething( ); TextField t; /*swing version: JTextField t; JtextField is defined in javax.swing */ t=new TextField("Initial text"); //swing version: //t=new // JTextField // ("Initial text"); //Event handlers are in //java.awt.event... t.addactionlistener(d); t.addkeylistener(d); f=new Frame("Hello, I am" + "a Frame"); //swing version: //f=new JFrame // ("Hello, I am" + // "a Frame"); f.setsize(800,500); //Color is in java.awt f.setbackground(color.orange); // swing version: // setbackground // (Color.orange); f.add("north",t); // swing version // add("north",t); That s It??! What do you imagine happens after main() finishes? 2
Java Programs Hanging Around Not! up to no gooddrinking beercausing trouble They wait for events JRE finds out about it from OS JRE sends program a data object The program reacts to the event It is event driven Event Driven Programming Let s look at: the event driven part of the CharCheck program Recall part of it public class CharCheck{ static Frame f; //swing version: JFrame f; public static void main (String args[ ]){ DoSomething d=new DoSomething( ); Part we ve seen + 2 new lines f.add("north",t); // swing version // add("north",t); The new stuff, continued public void actionperformed (ActionEvent e){ System.out.println (e.getactioncommand( )); //runs after typing the return key 3
The new stuff, continued public void keypressed(keyevent k){ System.out.println ("next char is " +(char)(1+k.getkeychar( ))); //runs when a key is pressed The new stuff (end) public void keyreleased(keyevent k){ System.out.println ("number of char is " +(int)k.getkeychar( )); //runs when a key is released Where does output go? Output is to (1) a frame, and (2) the window from which the program was run Initial display on next slide After typing in some characters: System.out.println( ) output is: 4
System.out.println( ) output is:.. A few more comments on Event Handlers We needed two: Can't extend (inherit) more than one, so the other must say implements (it s an interface) Java (JRE) knows what method to call when an event occurs That is why all interface method prototypes must be implemented A few more comments on Event Handlers We needed two: Can't extend (inherit) more than one, so the other must say implements (it s an interface) Java (JRE) knows what method to call when an event occurs That is why all interface method prototypes must be implemented AWT= "Abstract Windowing Toolkit" Contains many "heavyweight" components Component: a GUI item (buttons, text fields...) Heavyweight: refers to a GUI item that relies on the platform-provided windowing capabilities Problem: a Button looks different on Macs, and MS systems - Java tries to be platform-independent! Lightweight: refers to a platform-independent GUI component Example: a JButton is lightweight Swing Has (mostly) lightweight components Used to be "experimental" Hence, import javax.swing.*; The "x" indicated it was experimental...so, why is the "x" still there?? If you use swing, must also import awt There are more swing than awt components Example: TextField JTextField JPasswordField (no "PasswordField" class exists) - a JPasswordField is like a JTextField but doesn't show what characters are typed AWT vs. Swing 5