Course: CMPT 101/104 E.100 Thursday, November 23, 2000 Lecture Overview: Week 12 Announcements Assignment 6 Expectations Understand Events and the Java Event Model Event Handlers Get mouse and text input in applications Frame Windows Adding interface component to frame windows To Do: Review Chapter 10 Read ahead: Chapter 12 1
Events DOS / Unix is kind of comforting but those days are over. With this kind of program the user can only do 1 thing in practice: type in commands or data. This is easy for a programmer to deal with. You can basically force the user to follow a pre-determined path of execution, step by step. For example, it is easy to enforce typical program logic for the Bank programs such as this. Start If the user enters 1, create a new account Else, if the user enters 7, Quit the program Else the input is invalid. Display error message. You can see that the user has one and only one option at any given time: keyboard input. Under these circumstances the programmer has it easy. 2
A typical window application is much more complex: There are multiple controls, each behaving in a different fashion. Some of the controls should only be enabled at certain times. Some of the controls should be populated with data at runtime. Which control does the user push, select, click, or activate? In what order? You probably cannot appreciate how much more complicated this sort of application is than the former, or how much design and testing that must go into these, nevertheless; the days of DOS are pretty well gone. The windows application is the majority of the work out there. 3
There are many different types of events: user types in data user clicks (double-clicks) on a mouse key: (left, right middle?) user presses a special key user resizes window user closes / minimizes window user tabs in or out of an text box etc The operating system will send messages to your program when an event occurs, though most events are generally ignored. To handle an event you need to create an event handler object from a special class. Before considering the complexities let us look at the Event Class Hierarchy: (p. 397): EventObject AWTEvent ActionEvent ComponentEvent InputEvent WindowEvent MouseEvent KeyEvent As you can see, one size does not fit all. If you want to handle events from a particular source you must create an object of the appropriate type. 4
Insert Addendum here. 5
Implementing the Interface The key to handling events is implementing the appropriate listener interface. For example, If you want to receive notifications about mouse events you need to implement the MouseListener interface. The Java Runtime sends you notification of the mouse events by calling the methods that are defined in the MouseListener Interface. That is why you must implement the interface: so that runtime can send you notification of mouse events by calling the methods that you have implemented. Here is that interface definition: public interface MousListener { // p 398 void mouseclicked(mouseevent event); void mouseentered(mouseevent event); void mouseexisted(mouseevent event); void mousepressed(mouseevent event); void mousereleased(mouseevent event); // You can probably guess what these are for. Notice: A parameter object of the MouseEvent class is sent to you with each method. This class has a set of methods that will make your life easier, such as the getx() and gety() methods that will allow you to determine where the mouse cursor is pointing at a given time. In order to get a feel for how these interfaces work, let us consider the applet created in the book: 6
You will see that this applet does little more than implement the mouse interface. import java.applet.applet; import java.awt.event.mouseevent; import java.awt.event.mouselistener; public class MouseSpyApplet extends Applet { public MouseSpyApplet() { // applet constructor MouseSpy listener = new MouseSpy(); addmouselistener(listener); // this tells the runtime where to send the event notifications class MouseSpy implements MouseListener { public void mouseclicked(mouseevent event) { System.out.println("Mouse clicked: x = " + event.getx() + " y = " + event.gety()); public void mouseentered(mouseevent event) { System.out.println("Mouse entered: x = " + event.getx() + " y = " + event.gety()); public void mouseexited(mouseevent event) { System.out.println("Mouse exited: x = " + event.getx() + " y = " + event.gety()); public void mousepressed(mouseevent event) { System.out.println("Mouse pressed: x = " + event.getx() + " y = " + event.gety()); public void mousereleased(mouseevent event) { System.out.println("Mouse released: x = " + event.getx() + " y = " + event.gety()); 7
Overriding Those Methods Overriding interface methods can be tedious, and they are always abstract, which means that you must override them all. Most of the time you don't want your class to do anything with a given method. For example, you generally won't want to do anything special when the mouse enters the area of the applet window, so your implementation would look something like this: public void mouseentered(mouseevent event) { // do nothing Because you rarely want to do anything with all or most of the methods of a given interface, some friendly soul at Sun created a special class that implements every listener interface with more than 1 method: the Adaptor classes. For example, there is a MouseAdapter class that implements that MouseListener interface with methods that do nothing. By creating a subclass of the appropriate Adaptor class (instead of implementing the listener interface from scratch) you only need to override the methods that you actually want to use, rather than having to override many unneeded methods as would be the case normally. // here is what the MouseAdaptor class looks like public class MouseAdapter implements MouseListener { public void mouseclicked(mouseevent event) { public void mouseentered(mouseevent event) { public void mouseexited(mouseevent event) { public void mousepressed(mouseevent event) { public void mousereleased(mouseevent event) { 8
Listeners as Inner Classes You can now create a class that receives event notifications from the Java Runtime. So what? In order to do something useful and interesting, listener objects need to interact with other objects, and those objects are usually private. This poses a problem. In the following example you will see that the listener class and the EggApplet class are completely separate. The listener object cannot change the location of the ellipse that is the egg because it is private: public class EggApplet extends Applet{ // Constants, variables, etc. private Ellipse2D.Double egg; MouseSpy listener = new MouseSpy(); addmouselistener(listener); // etc.. class MouseClickListener extends MouseAdapter { // MouseClickListener objects have not access // to the egg! 9
Source Code Solution A // Put the listener class inside the EggApplet class import java.applet.applet; import java.awt.graphics; import java.graphics2d; import java.awt.geom.ellipse2d; import java.awt.event.mouseevent; import java.awt.event. MouseAdapter; // create an EggApplet class public class EggApplet extends Applet{ // Constants, variables, etc. private Ellipse2D.Double egg; private static final double EDD_WIDTH = 15; private static final double EDD_HEIGHT = 25; // constructor public EggApplet() { egg = new Ellipse2D.Double(0,0,EGG_WIDTH, EGG_HEIGHT); MouseClickListener listener = new MouseClickListener(); addmouselistener(listener); public void paint(graphics g) { Graphics2D g2 = (Graphics2D)g; g2.draw(egg); // The lister class is declared inside the applet. private class MouseClickListener extends MouseAdapter { // only override one method public void mouseclicked(mouseevent event) { int mousex = event.getx(); int mousey = event.gety(); egg.setframe(mousex EGG_WIDTH/2, mousey EGG_HEIGHT/2, EGG_WIDTH, EGG_HEIGHT); repaint(); 10
Notes from the Applet The constructor of the applet creates an instance of the inner class. The inner class will remember the object in which it was constructed. MouseClickListener listener = new MouseClickListener(); The inner class has no variable named egg, so it looks in the outer class for the egg variable and finds it. Because the MouseClickListener object is an object of the inner class it can freely access the private variable egg. egg.setframe(mousex EGG_WIDTH/2, mousey EGG_HEIGHT/2, EGG_WIDTH, EGG_HEIGHT); setframe() is an ellipse method that updates the position and size of the ellipse. repaint() tells the Java Runtime to tell the Window manager to call the paint() method so that the ellipse can be redrawn in its new position. You could call the paint() method directly yourself since it is a public method of the applet, but this you should never do. Other activities are probably going on while you are running your applet. Let the Window Manager paint() the applet again when it is convenient. The repaint() method belongs to the applet class. It does not show up in the previous definition because we did not override it. Note: objects created from inner classes are like instance variables of the outer class. They can see and use all methods and variables of both themselves and the outer class. You will use inner classes most frequently for event listeners. 11
Frame Windows Let's create a windows application that is not an applet. Such an application does not run inside a browser such as Netscape, but deals with the Operating System through the Java Runtime. Here is the code that would create 2 simple windows: // filename: FrameTest1.java import javax.swing.jframe; public class FrameTest1 { public static void main(string [] args) { EmptyFrame frame1 = new EmptyFrame(); frame1.settitle("frame Test"); frame1.show(); EmptyFrame frame2 = new EmptyFrame(); frame2.settitle("second Frame"); frame2.show(); System.out.println("\n\nMain method is terminating, but Window remains."); // System.exit(0); // This is the simple window class class EmptyFrame extends JFrame { public EmptyFrame(){ // constructor final int DEFAULT_FRAME_WIDTH = 300; // pixels final int DEFAULT_FRAME_HEIGHT = 300; // pixels setsize(default_frame_width, DEFAULT_FRAME_HEIGHT); 12
Notes about FrameTest1.java You will notice that the main method ends but the two windows continue to exist. When any window is created it receives it's own thread. A thread is like a unit of execution in the operating system. You will also notice that the program does not stop running until I press CTRL-C. While the main() method thread has finished the thread that display the windows to you must be manually killed. You will notice that the window has a border and a title bar. All Java Frames have these two things, though this is not necessarily the case for other programming languages. The applet has a border but no title bar. (The title bar of the applet viewer is what you see when you use that program.) This program uses the javax.swing package. A few years ago you used components from the AWT package to create windows, but this created problems. The AWT used frames, buttons, and other components from the native platforms it ran on. The frames, buttons, entry fields, and other components that Microsoft, Apple, and Unix offered where similar to eachother in most respects, but just different enough from eachother to cause lots of headaches for programmers. 'Write once run everywhere' became 'write once, debug everywhere'. The javax (Java extensions) package literally paints every component that it offers from scratch. This makes it a bit slow but very consistent. 13
Killing your program The System.exit(0); call will terminate your program and the runtime, but when should you call this? frame.show(); System.exit(0); Putting this command at the end of the main method will cause the windows to show for a split second and then close as the runtime exits. The ideal solution is to only have the program terminate when both windows are closed. How do we accomplish this? Step 1: Implement the WindowListener interface Just as applets can receive event notifications, so can windows if they implement the WindowListener interface. The abstract methods of the above interface are given below: public interface WindowListener { void windowopened(windowevent e); void windowclosed(windowevent e); void windowactivated(windowevent e); void windowdeactivated(windowevent e); void windowiconified(windowevent e); void windowdeiconified(windowevent e); void windowclosing(windowevent e); Just as there is a MouseAdaptor class that implements the MouseListener methods for you, there is a WindowAdaptor class that does the same thing. 14
Closing the Program 1) Suppose we add a class variable to the EmptyFrame class that keeps track of how many windows are open at a given time: private static int window_count = 0; 2) We increment that variable in the constructor of the EmptyFrame class: window_count++; 3) Suppose we create an event handler that decrements the count and kills the program if the count reaches 0. We will make that class an inner class of the EmptyFrame class: 4) Notice that you need to create an event handler for each window that you open. 5) One problem of using adaptor classes is that minor typos can lead to strange problems. For example, if you type the windowclosing method as WindowClosing you will have created a new method that does not receive the window event notification. 15
FrameTest2.java import javax.swing.jframe; import java.awt.event.windowevent; import java.awt.event.windowadapter; public class FrameTest2 { public static void main(string [] args) { EmptyFrame frame1 = new EmptyFrame(); frame1.settitle("frame Test"); frame1.show(); EmptyFrame frame2 = new EmptyFrame(); frame2.settitle("second Frame"); frame2.show(); // This is the slightly modified simple window class class EmptyFrame extends JFrame { private static int window_count = 0; // Note 1 public EmptyFrame(){ // constructor final int DEFAULT_FRAME_WIDTH = 300; // pixels final int DEFAULT_FRAME_HEIGHT = 300; // pixels setsize(default_frame_width, DEFAULT_FRAME_HEIGHT); WindowCloser listener = new WindowCloser(); addwindowlistener(listener); // Note 4 System.out.println("Added Window Listener"); window_count++; // Note 2 System.out.println("New window #" + window_count); // this is the event handler private class WindowCloser extends WindowAdapter { public void windowclosing(windowevent e){ // Note 3, 5 window_count--; System.out.println("Window #" + window_count + " closed"); if (window_count < 1) { System.exit(0); 16
Adding Components to JFrames JFrame Components are more complex than applet frames. With applets you draw right onto the applet frame. JFrames are actually composed of several layers: Titlebar Menu bar (optional) JFrame Root Pane Holds the glass pane and the content pane together Layered Pane The Content Pane and the Menu bar are mounted on the Layered Pane. Glass Pane The primary purpose of the glass pane is capture mouse events. The glass pane is transparent. Content Pane This one holds the components that you will put on the window 17
Adding a Component to the Window To add a component to a window you need to create a panel and place it on the Content Pane. // filename FrameTest3 import javax.swing.jframe; import javax.swing.jpanel; import java.awt.component; import java.awt.container; import java.awt.graphics; import java.awt.graphics2d; public class FrameTest3 { public static void main(string [] args) { MyFrame frame = new MyFrame(); frame.settitle("one Centered Panel"); frame.show(); class MyPanel extends JPanel { // This is the panel that we will put on the window public void paintcomponent(graphics g) { super.paintcomponent(g); Graphics2D g2 = (Graphics2D)g; g2.drawstring("hi there!", 50, 50); class MyFrame extends JFrame { public MyFrame() { // constructor MyPanel panel = new MyPanel(); Container contentpane = getcontentpane(); contentpane.add(panel, "Center"); final int DEFAULT_FRAME_WIDTH = 300; // pixels final int DEFAULT_FRAME_HEIGHT = 300; // pixels setsize(default_frame_width, DEFAULT_FRAME_HEIGHT); 18
Notes: FrameTest3.java Notice that while the applet has a paint method, the JPanel has a paintcomponent method. public void paintcomponent(graphics g) { super.paintcomponent(g); Also notice the call to the superclass method: paintcomponent( ). Every javax.swing component inherits this method and should call the superclass method. The superclass paintcomponent method will erase the previous contents of the panel, insure that the borders of the panel are drawn and the attributes of the graphics object g are set correctly. Panel Positioning: contentpane.add(panel, "Center"); Notice that a panel can only be positioning loosely on the Content Pane. The only options available are North, Center, South, East and West. (see page 416) 19
Reading in Text Input The final example of the chapter deals with reading in text from a JTextField control. These are very common and worth considering in some detail. Notes: The program has the typical JFrame component. The JFrame component has a private object instance: that of a JTextField. import javax.swing.jframe; import javax.swing.jtextfield; class MyFrame extends JFrame { // various declarations private JTextField textfield; // etc.. This text field is added to the window (MyFrame) at the bottom of the window: Container contentpane = getcontentpane(); contentpane.add(textfield, "South"); 20
Action Events Mouse events are more complex than the events that can occur in a text box, particularly in Java. While some languages allow you receive an event for every time the user presses a key while in the text box, the JTextField component only generates an event when the returns presses ENTER. This results in an Action event. To handle this event you will need to implement the ActionListener interface. This interface only has a single method: public interface ActionListener { public void actionperformed(actionevent event); The ActionEvent object contains various bits of information, such as which component generated the event, and what kind of event occured. We will look at this class in more detail next week, however; for now we know that we only have the JTextField that will be generating events. Here is the class that implements the interface. Notice the gettext() and the settext() methods that are part of the JTextField class... private class TextFieldListener implements ActionListener { public void actionperformed(actionevent event) { String input = textfield.gettext(); // do some stuff textfield.settext(""); 21
Implementation Let's walk through the code. This program allows you to enter data (an integer) into a textbox, then paints that many eggs (ellipses) at random locations in the window. // filename: Eggs.java /* Note: you need a lot of classes, so the import section is long. This is typical of modern software programs: no one person writes the program, but rather teams of people do. Even this simple program requires the work of the developers at Sun. */ import java.awt.container; import java.awt.graphics; import java.awt.graphics2d; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.awt.event.windowadapter; import java.awt.event.windowevent; import java.awt.geom.ellipse2d; import java.util.random; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jtextfield; /* Question 1: do you understand what role each of these classes will play? It is important to understand that each of the above classes represents an Entity. You must accept the notion that each of the above items has an individual nature. Each has attributes and behaviour (variables and methods) associated with it that you do not know about. */ 22
/* Notice how simple the Eggs class is. All it does is provide a place for the program to start, creates an EggFrame object, sets an attribute, and displays it to the user. Then the main() method ends. The EggFrame object does all the rest of the work itself. It truly acts as an independent entity with a life of its own. This is quite a departure from the practice of procedural programming where the main method is the source of most every command and action. */ public class Eggs { public static void main(string[] args) { EggFrame frame = new EggFrame(); frame.settitle("enter a number of eggs"); frame.show(); // end of Eggs class /* Here is the EggFrame class. This class is used to create the window object that will be displayed. This class contains classes of its own. An EggFrame object will contain other objects. An EggFrame object will handle it's own events. */ class EggFrame extends JFrame { // This frame class will contain a text field object mounted on // a panel object. private JTextField textfield; private EggPanel panel; 23
/* Event Handler class Notice how this class has one purpose only: it kills (exits, closes, destroys, etc.) this program when it receives the windowclosing() event. (How does it receive this event?) This is a very simple, inner class that belongs to the EggFrame class. It is used by the EggFrame class to close the program when the EggFrame window is closed by the user. */ private class WindowCloser extends WindowAdapter { public void windowclosing(windowevent event){ System.exit(0); // default constructor public EggFrame() { final int DEFAULT_FRAME_WIDTH = 300; final int DEFAULT_FRAME_HEIGHT = 300; /* NOTE: you always find these specifications laid out as constants. Magic Numbers are the kiss of death in the real world of programming. */ // This method is inherited. setsize(default_frame_width, DEFAULT_FRAME_HEIGHT); //WindowCloser closer = new WindowCloser(); //addwindowlistener(closer); addwindowlistener(new WindowCloser()); /* Notice the above method call. The statement new WindowCloser() returns a reference to a object that implements the WindowListener interface. You pass this reference to the addwindowlistener method. Notice that an an un-named object now exists as our Window Event handler, but the handler is simple so we don't care to keep track of it. */ panel = new EggPanel(); contentpane.add(panel, "Center"); textfield = new JTextField(); textfield.addactionlistener(new TextFieldListener()); contentpane.add(textfield,"south"); 24
/* Text Field event handler: another inner class We will use this class to handle events from the the text field. Actually, we could use this class for any text field. We know that an event occurs when the user enters text and presses ENTER. */ private class TextFieldListener implements ActionListener { // How does an ActionListener differ from a MouseListener? public void actionperformed(actionevent event) { String user_input = textfield.gettext(); int number = Integer.parseInt(user_input); // could an Exception be thrown? // If so, why doesn't the compiler complain? panel.seteggcount(number); textfield.settext(""); // where did this method come from? What does it do? // Why can the textfield be referenced here? 25
/* Here is the class that we draw eggs (Ellipses) on. */ class EggPanel extends JPanel { private int eggcount = 0; private static final double EGG_WIDTH = 30; private static final double EGG_HEIGHT = 50; // What is gained here in using constants? public void paintcomponent(graphics g) { super.paintcomponent(g); // why call the superclass' method? Graphics2D g2 = (Graphics2D)g; // what is happening here? Why do it? Random generator = new Random(); for (int ii = 0; ii < eggcount; ii++) { double x = getwidth() * generator.nextdouble(); double y = getheight() * generator.nextdouble(); Ellipse2D.Double egg = new Ellipse2D.Double(x,y, EGG_WIDTH, EGG_HEIGHT); g2.draw(egg); /* This method provides a way for the private variable eggcount to be set. */ public void seteggcount(int count) { eggcount = count; repaint(); // why not call paintcomponent directly? // end EggPanel class // end EggFrame class Note: this is a long, gritty program if you are new to the world of UI and components. I recommend that you type it in and compile it yourself. 26