// This example is from the book _Java in a Nutshell_ by David Flanagan. // Written by David Flanagan. Copyright (c) 1996 O'Reilly & Associates. // You may study, use, modify, and distribute this example for any purpose. // This example is provided WITHOUT WARRANTY either expressed or implied. import java.awt.*; public class AllEvents extends AllComponents { public AllEvents(String title) { super(title); } // This method handles all the events generated by all the components. public boolean handleEvent(Event event) { switch(event.id) { // Most components generate ACTION_EVENT // We test the target field to find out which component. case Event.ACTION_EVENT: if (event.target == textfield) { textarea.setText("Your name is: " + (String)event.arg + "\n"); } else if (event.target == choice) { textarea.setText("Your favorite color is: " + (String) event.arg + "\n"); } else if ((event.target == checkboxes[0]) || (event.target == checkboxes[1]) || (event.target == checkboxes[2])) { textarea.setText("Your favorite flavor is: " + checkbox_group.getCurrent().getLabel() + "\n"); } else if (event.target == list) { textarea.setText("You double-clicked on: " + (String)event.arg + "\n"); } else if (event.target == okay) { textarea.setText("Okay button clicked.\n"); } else if (event.target == cancel) { textarea.setText("Cancel button clicked.\n"); } else if (event.target instanceof MenuItem) { // Since we didn't save references to each of the menu objects, // we check which one was pressed by comparing labels. // Note that we respond to these menu items by // popping up dialog boxes. if (((String)event.arg).equals("Quit")) { YesNoDialog d = new ReallyQuitDialog(this, textarea); d.show(); } else if (((String)event.arg).equals("Open")) { textarea.setText("You selected Open.\n"); // Use the dialog box created by our superclass. file_dialog.pack(); // bug workaround file_dialog.show(); // blocks until user selects a file textarea.setText("You selected file: " + file_dialog.getFile()); } else if (((String)event.arg).equals("About")) { InfoDialog d; d = new InfoDialog(this, "About AWT Demo", "This demo was written by David Flanagan\n" + "Copyright (c) 1996 O'Reilly & Associates"); d.show(); textarea.setText("You selected About.\n"); } } else { textarea.setText("Unknown action event."); } break; // Double-clicking on a list generates an action event. // But list selection and deselection are separate event types. case Event.LIST_SELECT: textarea.setText("You selected: " + list.getItem(((Integer)event.arg).intValue()) + "\n"); break; case Event.LIST_DESELECT: textarea.setText("You deselected: " + list.getItem(((Integer)event.arg).intValue()) + "\n"); break; // These are some events pertaining to the window itself. case Event.WINDOW_DESTROY: textarea.setText("Window Destroy\n"); break; case Event.WINDOW_ICONIFY: textarea.setText("Window iconify\n"); break; case Event.WINDOW_DEICONIFY: textarea.setText("Window deiconify\n"); break; case Event.WINDOW_MOVED: textarea.setText("Window moved\n"); break; // We print a message about each of these mouse and key events, // but return false after so that they can still // be properly handled by their correct recipient. case Event.MOUSE_DOWN: textarea.setText("Mouse down: [" + event.x + "," + event.y + "]\n"); return false; case Event.MOUSE_UP: textarea.setText("Mouse up: [" + event.x + "," + event.y + "]\n"); return false; case Event.MOUSE_DRAG: textarea.setText("Mouse drag: [" + event.x + "," + event.y + "]\n"); return false; case Event.KEY_PRESS: case Event.KEY_ACTION: textarea.setText("Key press\n"); return false; case Event.KEY_RELEASE: case Event.KEY_ACTION_RELEASE: textarea.setText("Key release\n"); return false; // We ignore these event types. case Event.GOT_FOCUS: case Event.LOST_FOCUS: case Event.MOUSE_ENTER: case Event.MOUSE_EXIT: case Event.MOUSE_MOVE: return false; // We shouldn't ever get this... default: textarea.setText("Unexpected Event type: " + event + "\n"); break; } return true; } // Here's the method that lets us run and test this class. public static void main(String[] args) { Frame f = new AllEvents("AWT Demo"); f.pack(); f.show(); } }