GUI and EventDriven Programming Part 2 Event Handling

  • Slides: 27
Download presentation
GUI and Event-Driven Programming Part 2

GUI and Event-Driven Programming Part 2

Event Handling l l An action involving a GUI object, such as clicking a

Event Handling l l An action involving a GUI object, such as clicking a button, is called an event. The mechanism to process events is called event handling. The event-handling model of Java is based on the concept known as the delegation-based event model. With this model, event handling is implemented by two types of objects: – – event source objects event listener objects.

Event Sources and Listeners l l An event source object is a GUI object

Event Sources and Listeners l l An event source object is a GUI object where an event occurs. An event source generates events. An event listener object is an object that includes a method that gets executed in response to the generated events When an event is generated, the system notifies the relevant event listener objects.

Events and Event Listeners l l l There are many different kinds of events,

Events and Event Listeners l l l There are many different kinds of events, but the most common one is an action event. For the generated events to be processed, we must associate, or register, event listeners to the event sources. If event sources have no registered listeners, the generated events are ignored.

Events and Event Listeners l l An object that can be registered as an

Events and Event Listeners l l An object that can be registered as an action listener must be an instance of a class that is declared specifically for the purpose. We call such classes action listener classes. To associate an action listener to an action event source, we call the event source’s add. Action. Listener method with the action listener as its argument.

Events and Event Listeners l l l A single listener can be associated to

Events and Event Listeners l l l A single listener can be associated to multiple event sources. Likewise, multiple listeners can be associated to a single event source. When an event source generates an event, the system checks for matching registered listeners. – – If there is no matching listener, the event is ignored. If there is a matching listener, the system notifies the listener by calling the listener’s corresponding method.

Action Listener Classes l l In the case of action events, the method called

Action Listener Classes l l In the case of action events, the method called is action. Performed. To ensure the programmer includes the necessary action. Performed method in the action listener class, the class must be defined in a specific way. import java. awt. event. *; public class Button. Handler implements Action. Listener {. . . }

The Action. Listener interface l l Action. Listener is an interface, not a class.

The Action. Listener interface l l Action. Listener is an interface, not a class. Like a class, an interface is a reference data type, but unlike a class, an interface includes only constants and abstract methods. An abstract method has only the method header, or prototype; it has no method body. A class that implements a Java interface must provide the method body to all the abstract methods defined in the interface.

The Action. Listener interface l l By requiring an object we pass as an

The Action. Listener interface l l By requiring an object we pass as an argument to the add. Action. Listener method to be an instance of a class that implements the Action. Listener interface, the system ensures that this object will include the necessary action. Performed method. Implementation of an interface is Java’s mechanism for providing a form of multiple inheritance

Example l l Suppose we want to change the title of the frame, depending

Example l l Suppose we want to change the title of the frame, depending on which button is clicked We can do this using the action. Performed method. The method model is: public void action. Performed(Action. Event evt) { String button. Text = get the text of the event source; JFrame frame = the frame that contains this event source; frame. set. Title(“You clicked “ + button. Text); }

Handling Button Events l l The first statement may be handled in one of

Handling Button Events l l The first statement may be handled in one of two ways. The first way is via the get. Action. Command method of the action event object evt: String button. Text = evt. get. Action. Command(); l The second way is via the get. Source method of the action event object evt. : JButton clicked. Button = (JButton) evt. get. Source(); String button. Text = clicked. Button. get. Text();

Handling Button Events l l A frame window contains nested layers of panes. The

Handling Button Events l l A frame window contains nested layers of panes. The topmost pane is called the root pane. To find the frame that contains the event source, we – – get the root pane to which the event source belongs, then get the frame that contains this root pane. JRoot. Pane root. Pane = clicked. Button. get. Root. Pane(); Frame frame = (JFrame) root. Pane. get. Parent(); l The frame can be the event listener of the GUI objects it contains. cancel. Button. add. Action. Listener(this); ok. Button. add. Action. Listener(this);

Swing Classes for Text Handling l The Swing GUI classes JLabel, JText. Field, and

Swing Classes for Text Handling l The Swing GUI classes JLabel, JText. Field, and JText. Area all deal with text. – – – A JLabel object displays uneditable text. A JText. Field object allows the user to enter a single line of text. A JText. Area object allows the user to enter multiple lines of text. It can also be used for displaying multiple lines of uneditable text.

Text Fields and Action Events l l l An instance of JText. Field generates

Text Fields and Action Events l l l An instance of JText. Field generates action events when the object is active and the user presses the ENTER key. The action. Performed method must determine which of the event sources (for example, a button or a text field) generated the event. The instanceof operator determines the class to which the event source belongs. The general model is thus: if (event. get. Source() instanceof JButton ){ // event source is either cancel. Button // or ok. Button. . . } else { // event source must be input. Line. . . }

Text Fields and Action Events l The get. Text method of JText. Field may

Text Fields and Action Events l The get. Text method of JText. Field may be used to retrieve the text that the user entered. public void action. Performed(Action. Event event) { if (event. get. Source() instanceof JButton){ JButton clicked. Button = (JButton) event. get. Source(); String button. Text = clicked. Button. get. Text(); set. Title(“You clicked ” + button. Text); } else {// the event source is input. Line set. Title(“You entered ‘” + input. Line. get. Text() + “’”); } }

JLabel Objects l A JLabel object is useful for displaying a label indicating the

JLabel Objects l A JLabel object is useful for displaying a label indicating the purpose of another object, such as a JText. Field object. prompt = new JLabel(“Please enter your name”); l l l JLabel objects may also be used to display images. When a JLabel object is created, we can pass an Image. Icon object instead of a string. To create the Image. Icon object, we must specify the filename of the image.

JLabels and Images l We declare the data member private JLabel image; l then

JLabels and Images l We declare the data member private JLabel image; l then create it in the constructor as public Text. Frame 2 () {. . . image = new JLabel(new Image. Icon(“cat. gif”); // assumes the. gif is/in the same directory as the program image. set. Bounds(10, 20, 50); content. Pane. add(image); . . . }

Example with JLabel, JButton and JText. Field objects

Example with JLabel, JButton and JText. Field objects

Text. Area & Related Methods l l A Text. Area is like a Text.

Text. Area & Related Methods l l A Text. Area is like a Text. Field, but can accommodate more text Methods include: set. Border(Border. Factory); // creates a border around a JText. Area object l set. Editable(boolean); // enables/disables changes to a JText. Area object l l l The append(String) method allows us to add text to the text area without replacing old content. Using the set. Text (String) method of JText. Area will replace old content with new content.

Example frame with JText. Area object

Example frame with JText. Area object

Adding Scrollbars to a JText. Area JScroll. Pane scroll. Text = new JScroll. Pane(text.

Adding Scrollbars to a JText. Area JScroll. Pane scroll. Text = new JScroll. Pane(text. Area); content. Pane. add(scroll. Text);

Menus l The javax. swing package contains three useful menu-related classes: JMenu. Bar, JMenu,

Menus l The javax. swing package contains three useful menu-related classes: JMenu. Bar, JMenu, and JMenu. Item. – – – Menu. Bar is a bar where the menus are placed. Menu (such as File or Edit) is a single item in the Menu. Bar. Menu. Item (such as Copy, Cut, or Paste) is an individual menu choice in the Menu.

Menus l l l When a Menu. Item is selected, it generates a menu

Menus l l l When a Menu. Item is selected, it generates a menu action event. We process menu selections by registering an action listener to menu items. The following example will create the two menus shown and illustrate how to display and process the menu item selections.

Menus l We will follow this sequence of steps: Create a JMenu. Bar object

Menus l We will follow this sequence of steps: Create a JMenu. Bar object and attach it to a frame. 2. Create a JMenu object. 3. Create JMenu. Item objects and add them to the JMenu object. 4. Attach the JMenu object to the JMenu. Bar object. 1.

Menus l We create a file. Menu object as file. Menu = new Menu(“File”);

Menus l We create a file. Menu object as file. Menu = new Menu(“File”); l l The argument to the Menu constructor is the name of the menu. Menu items appear from the top in the order in which they were added to the menu.

Menus l To create and add a menu item New to file. Menu, we

Menus l To create and add a menu item New to file. Menu, we execute item = new Menu. Item(“New”); item. add. Action. Listener(this); file. Menu. add(item); l And to add a horizontal line as a separator between menu items, we execute file. Menu. add. Separator();

Menus l l We then attach the menus to a menu bar. We create

Menus l l We then attach the menus to a menu bar. We create a Menu. Bar object in the constructor, call the frame’s set. Menu. Bar method to attach it to the frame, and add the two Menu objects to the menu bar. Menu. Bar menu. Bar = new Menu. Bar(); set. Menu. Bar(menu. Bar); menu. Bar. add(file. Menu); menu. Bar. add(edit. Menu);