Chapter 7 Book Chapter 14 GUI and EventDriven

Chapter 7 (Book Chapter 14) GUI and Event-Driven Programming ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 1

Objectives • After you have read and studied this chapter, you should be able to – Define a subclass of JFrame to implement a customized frame window. – Write event-driven programs using Java's delegation-based event model – Write GUI application programs using JButton, JLabel, Image. Icon, JText. Field, and JText. Area. – Understand GUI application programs with menus – Understand GUI application programs that process mouse events

Graphical User Interface • In Java, GUI-based programs are implemented by using classes from the javax. swing and java. awt packages. • The Swing classes provide greater compatibility across different operating systems. They are fully implemented in Java, and behave the same on different operating systems. ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 3

Sample GUI Objects • Various GUI objects from the javax. swing package. ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 4

JOption. Pane • Using the JOption. Pane class is a simple way to display the result of a computation to the user or receive an input from the user. • We use the show. Message. Dialog class method for output. • If we pass null as the first argument, the dialog appears on the center of the screen. • If we pass a frame object as the first argument, the dialog is positioned at the center of the frame. ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 5

Using JOption. Pane for Output import javax. swing. *; . . . JOption. Pane. show. Message. Dialog( null, “I Love Java” ); ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 6

Using JOption. Pane for Output - 2 import javax. swing. *; . . . JOption. Pane. show. Message. Dialog( null, “onentwonthree” ); //place newline n to display multiple lines of output ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 7

Using JOption. Pane for Output - 3 import javax. swing. *; class Ch 14 Show. Message. Dialog { public static void main(String[] args) { JFrame j. Frame; j. Frame = new JFrame( ); j. Frame. set. Size(400, 300); j. Frame. set. Visible(true); JOption. Pane. show. Message. Dialog(j. Frame, "How are you? "); JOption. Pane. show. Message. Dialog(null, "Good Bye"); j. Frame. set. Default. Close. Operation(JFrame. EXIT_ON_CLOSE); } } ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 8

Using JOption. Pane for Output - 3 After Pressing OK or closing the “How are you? ” dialog, the “Good Bye” dialog appears ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 9

JOption. Pane for Input-1 We use the show. Input. Dialog class method for input. This method returns the input as a String value so we need to perform type conversion for input of other data types import javax. swing. *; . . . String inputstr = JOption. Pane. show. Input. Dialog( null, “What is your name? ” ); ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 10

JOption. Pane for Input-2 import javax. swing. *; . . . String inputstr = JOption. Pane. show. Input. Dialog( null, “Enter Age: ” ); int age= Integer. parse. Int(inputstr); ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 11

Creating a Plain JFrame import javax. swing. *; class Ch 7 Default. JFrame { public static void main( String[] args ) { JFrame default. JFrame; default. JFrame = new JFrame(); default. JFrame. set. Visible(true); } } ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 12

Subclassing JFrame • To create a customized frame window, we define a subclass of the JFrame class. • The JFrame class contains rudimentary functionalities to support features found in any frame window. ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 13

Creating a Subclass of JFrame • To define a subclass of another class, we declare the subclass with the reserved word extends. import javax. swing. *; class Ch 7 JFrame. Subclass 1 extends JFrame {. . . } ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 14

Creating a Subclass of JFrame • An instance of Ch 14 JFrame. Subclass 1 will have the following default characteristics: – – The title is set to My First Subclass. The program terminates when the close box is clicked. The size of the frame is 300 pixels wide by 200 pixels high. The frame is positioned at screen coordinate (150, 250). • These properties are set inside the default constructor.

Displaying Ch 14 JFrame. Subclass 1 • Here's how a Ch 14 JFrame. Subclass 1 frame window will appear on the screen. ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 16

Creating a Subclass of JFrame import javax. swing. *; class Ch 14 JFrame. Subclass 1 extends JFrame { private static final int int FRAME_WIDTH FRAME_HEIGHT FRAME_X_ORIGIN FRAME_Y_ORIGIN = = 300; 200; 150; 250; public Ch 14 JFrame. Subclass 1( ) { //set the frame default properties set. Title ( "My First Subclass" ); set. Size ( FRAME_WIDTH, FRAME_HEIGHT ); set. Location ( FRAME_X_ORIGIN, FRAME_Y_ORIGIN ); //register 'Exit upon closing' as a default close operation set. Default. Close. Operation( EXIT_ON_CLOSE ); } ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 17

The Content Pane of a Frame • The content pane is where we put GUI objects such as buttons, labels, scroll bars, and others. • We access the content pane by calling the frame’s get. Content. Pane method. This gray area is the content pane of this frame. ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 18

Changing the Background Color • Here's how we can change the background color of a content pane to blue: Container content. Pane = get. Content. Pane(); content. Pane. set. Background(Color. BLUE); Source File: Ch 14 JFrame. Subclass 2. java ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 19

Placing GUI Objects on a Frame • There are two ways to put GUI objects on the content pane of a frame: – Use a layout manager • Flow. Layout • Border. Layout • Grid. Layout – Use absolute positioning • null layout manager ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 20

Placing a Button-1 • A JButton object a GUI component that represents a pushbutton. • Here's an example of how we place a button with Flow. Layout. • Flow. Layout places objects in the top-to-bottom, left -to right order. content. Pane. set. Layout( new Flow. Layout()); ok. Button = new JButton("OK"); cancel. Button = new JButton("CANCEL"); content. Pane. add(ok. Button); content. Pane. add(cancel. Button); ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 21

Placing a Button-2 import javax. swing. *; import java. awt. *; class Ch 14 JButton. Frame extends JFrame { private static final int FRAME_WIDTH private static final int FRAME_HEIGHT private static final int FRAME_X_ORIGIN private static final int FRAME_Y_ORIGIN = = 300; 200; 150; 250; private JButton cancel. Button; private JButton ok. Button; public static void main(String[] args) { Ch 14 JButton. Frame frame = new Ch 14 JButton. Frame(); frame. set. Visible(true); } ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 22

Placing a Button-3 public Ch 14 JButton. Frame() { Container content. Pane = get. Content. Pane( ); content. Pane. set. Layout(new Flow. Layout()); set. Size set. Resizable set. Title set. Location ( ( FRAME_WIDTH, FRAME_HEIGHT ); false ); "Program Ch 14 JButton. Frame" ); FRAME_X_ORIGIN, FRAME_Y_ORIGIN ); //create and place two buttons on the frame's content pane ok. Button = new JButton("OK"); content. Pane. add(ok. Button); cancel. Button = new JButton("CANCEL"); content. Pane. add(cancel. Button); //register 'Exit upon closing' as a default close operation set. Default. Close. Operation( EXIT_ON_CLOSE ); } } ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 23

Event Handling • An action involving a GUI object, such as clicking a button, is called an event. • The mechanism to process events is called event handling. • Event handling is implemented by two types of objects: – event source objects – event listener objects ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 24

Event Source and Event Listeners Objects • An event source is a GUI object where an event occurs. We say an event source generates events. • Buttons, text boxes, list boxes, and menus are common event sources in GUI-based applications. • An event listener object is an object that includes a method that gets executed in response to the generated events. • A listener must be associated, or registered, to a source, so it can be notified when the source generates events. ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 25

Connecting Source and Listener event source JButton event listener notify Handler register A listener must be registered to a event source. Once registered, it will get notified when the event source generates events. ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 26

Event Types • There are different event types and event listeners • Mouse events are handled by mouse listeners • Item selection events are handled by Item listeners • and so forth • Among the different types of events, the action event is the most common. – Clicking on a button generates an action event – Selecting a menu item generates an action event – and so forth • Action events are generated by action event sources and handled by action event listeners. ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 27

Handling Action Events • The class Button. Handler is a user-defined action event action. Performed eventsource listener (event handler). listener • It must. JButton implement the Interface Button Handler Action. Listener • It must include the method action. Performed • The method action. Performed includes the code we want to be executed in response to the add. Action. Listener generated events JButton button = new JButton("OK"); Button. Handler handler = new Button. Handler( ); button. add. Action. Listener(handler); ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 28

Action. Listener Interface • When we call the add. Action. Listener method of an event source, we must pass an instance of a class that implements the Action. Listener interface. • The Action. Listener interface includes one method named action. Performed. • A class that implements the Action. Listener interface must therefore provide the method body of action. Performed. • Since action. Performed is the method that will be called when an action event is generated, this is the place where we put a code we want to be executed in response to the generated events. ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 29

The Button. Handler Class import javax. swing. *; import java. awt. event. *; class Button. Handler implements Action. Listener {. . . public void action. Performed(Action. Event event) { // code to be executed in response to the generated events. . . } } ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 30

Container as Event Listener • Instead of defining a separate event listener such as Button. Handler, it is much more common to have an object that contains the event sources be a listener. – Example: We make this frame a listener of the action events of the buttons it contains. event listener event source ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 31

Ch 14 JButton. Frame. Handler. . . class Ch 14 JButton. Frame. Handler extends JFrame implements Action. Listener {. . . public void action. Performed(Action. Event event) { JButton clicked. Button = (JButton) event. get. Source(); String button. Text = clicked. Button. get. Text(); set. Title("You clicked " + button. Text); } } ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 32

Ch 14 JButton. Frame. Handler (complete program-1) import javax. swing. *; import java. awt. event. *; class Ch 14 JButton. Frame. Handler extends JFrame implements Action. Listener { private private static final int FRAME_WIDTH static final int FRAME_HEIGHT static final int FRAME_X_ORIGIN static final int FRAME_Y_ORIGIN JButton cancel. Button; JButton ok. Button; = = 300; 200; 150; 250; public static void main(String[] args) { Ch 14 JButton. Frame. Handler frame = new Ch 14 JButton. Frame. Handler(); frame. set. Visible(true); } ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 33

Ch 14 JButton. Frame. Handler (complete program-2) public Ch 14 JButton. Frame. Handler() { Container content. Pane = get. Content. Pane( ); set. Size (FRAME_WIDTH, FRAME_HEIGHT); set. Resizable (false); set. Title ("Program Ch 14 JButton. Frame. Handler"); set. Location (FRAME_X_ORIGIN, FRAME_Y_ORIGIN); content. Pane. set. Layout(new Flow. Layout()); ok. Button = new JButton("OK"); content. Pane. add(ok. Button); cancel. Button = new JButton("CANCEL"); content. Pane. add(cancel. Button); //registering this frame as an action listener of the two buttons cancel. Button. add. Action. Listener(this); ok. Button. add. Action. Listener(this); set. Default. Close. Operation( EXIT_ON_CLOSE ); } ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 34

Ch 14 JButton. Frame. Handler (complete program-3) public void action. Performed(Action. Event event) { JButton clicked. Button = (JButton) event. get. Source(); String button. Text = clicked. Button. get. Text(); this. set. Title("You clicked " + button. Text); } } ©The Mc. Graw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 14 - 35
- Slides: 35