Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interactive Programs Java API. Terminology Event—an action or occurrence, not part of a program, detected by the program. Events can be Event—an action.

Similar presentations


Presentation on theme: "Interactive Programs Java API. Terminology Event—an action or occurrence, not part of a program, detected by the program. Events can be Event—an action."— Presentation transcript:

1 Interactive Programs Java API

2 Terminology Event—an action or occurrence, not part of a program, detected by the program. Events can be Event—an action or occurrence, not part of a program, detected by the program. Events can be user actions--clicking a mouse button, pressing a key, hovering mouse over text box user actions--clicking a mouse button, pressing a key, hovering mouse over text box system occurrences—running out of memory, raising exceptions system occurrences—running out of memory, raising exceptions Event-driven program—responds to events, such as most programs in Windows environment Event-driven program—responds to events, such as most programs in Windows environment

3 Terminology (cont.) Event Listener— an interface (like a class, but provides only methods)—that will detect an event. The interface provides only an outline of the methods, and the programmer must write the code for each method. Event Listener— an interface (like a class, but provides only methods)—that will detect an event. The interface provides only an outline of the methods, and the programmer must write the code for each method. Event Handler— code that is executed when an even is detected Event Handler— code that is executed when an even is detected

4 To Make Interactive Program Steps to make a program interactive:. Steps to make a program interactive:. 1.Identify components with anticipated events (e.g., a button with a button- click) 2.Associate event listeners with the components 3.Write event handlers

5 Types of Events Action event Action event occurs when a button, check box, radio button, or text field is clicked. occurs when a button, check box, radio button, or text field is clicked. Adjustment event Adjustment event occurs occur when a scrollbar is adjusted by dragging an arrow on the bar or clicking anywhere on the bar. occurs occur when a scrollbar is adjusted by dragging an arrow on the bar or clicking anywhere on the bar. Focus event Focus event occurs when component gains or loses focus on the GUI. E.g., a text field can gain a focus by clicking the mouse anywhere inside it or by pressing the tab key occurs when component gains or loses focus on the GUI. E.g., a text field can gain a focus by clicking the mouse anywhere inside it or by pressing the tab key

6 Types of Events (cont.) Key event Key event occurs when a particular key is pressed on the keyboard. An event listener can distinguish each key so that appropriate event handlers can be provided. occurs when a particular key is pressed on the keyboard. An event listener can distinguish each key so that appropriate event handlers can be provided. Mouse event Mouse event occurs when the mouse is clicked over a component, when it hovers over a component, or when it leaves the component's area. occurs when the mouse is clicked over a component, when it hovers over a component, or when it leaves the component's area.

7 Event Listener Event listener—an interface corresponding to each type of event. Event listener—an interface corresponding to each type of event. When an event listener is associated with a class, the class must implement all methods in the event listener. When an event listener is associated with a class, the class must implement all methods in the event listener.

8 Event Listener (cont.) Interface ActionListener contains method actionPerformed(), to handle action events. Interface ActionListener contains method actionPerformed(), to handle action events. Interface AdjustmentListener contains method adjustmentValueChanged() to handle the adjustment events. Interface AdjustmentListener contains method adjustmentValueChanged() to handle the adjustment events. Interface FocusListener contains two methods: focusGained() and focusLost() to handle focus events. Interface FocusListener contains two methods: focusGained() and focusLost() to handle focus events. Interface KeyListener contains three methods: keyPressed(), keyReleased(), and keyTyped(), to handle key events. Interface KeyListener contains three methods: keyPressed(), keyReleased(), and keyTyped(), to handle key events. Interface MouseListener contains five methods: moustClicked(), mouseEntered(), mouseExited(), moustPressed(), and mouseReleased() to handle the mouse events. Interface MouseListener contains five methods: moustClicked(), mouseEntered(), mouseExited(), moustPressed(), and mouseReleased() to handle the mouse events.

9 Event Listener (cont.) NameMethod(s) ActionListeneractionPerformed() AdjustmentListeneradjustmentValueChanged() FocusListener focusGained(); focusLost() KeyListener keyPressed(); keyReleased(); keyTyped() MouseListener mouseClicked(); mouseEntered(); mouseExited(); moustPressed(); mouseReleased()

10 Implementing Interface To make use of the methods in an event listener interface in a class, it is implemented, instead of extended, by the class. To make use of the methods in an event listener interface in a class, it is implemented, instead of extended, by the class. If there are more than one interface to be brought in, they can be listed one after another with commas separating them. If there are more than one interface to be brought in, they can be listed one after another with commas separating them. The following code segment involves interfaces ActionListener and FocusListener in the EventDemo class. The following code segment involves interfaces ActionListener and FocusListener in the EventDemo class.

11 Implementing Interface (cont.) public class EventDemo extends JFrame implements ActionListener, FocusListener { implements ActionListener, FocusListener {... // code for EventDemo class... // code for EventDemo class}

12 Example: EventDemo.java The following is an interactive program that displays a message in a text field, whose background color can be changed by the user clicking one of the three buttons. The following is an interactive program that displays a message in a text field, whose background color can be changed by the user clicking one of the three buttons. In addition, the text style of the message is changed to italic when the field gains focus and returns to plain style when it loses focus. (The screen shot shows the window after the "cyan" button is clicked. Note that the text style is plain because the text field has lost its focus to the "cyan" button.) In addition, the text style of the message is changed to italic when the field gains focus and returns to plain style when it loses focus. (The screen shot shows the window after the "cyan" button is clicked. Note that the text style is plain because the text field has lost its focus to the "cyan" button.)

13 Example: EventDemo.java (cont.) Go to an Applet version of EventDemo

14 Example: EventDemo.java (cont.) Note Note In the class declaration, ActionListener and FocusListener are implemented. In the class declaration, ActionListener and FocusListener are implemented. Objects—buttons and textfield—are declared as instance variables (global to the class). Objects—buttons and textfield—are declared as instance variables (global to the class). In constructor EventDemo(), In constructor EventDemo(), buttons are associated with event listeners. E.g., green.addActionListener(this); buttons are associated with event listeners. E.g., green.addActionListener(this); Here, this refers the current class which will do the listening—like the “ear” Here, this refers the current class which will do the listening—like the “ear”

15 Example: EventDemo.java (cont.) Buttons are added to a container Buttons are added to a container Buttons must be associated with event listeners before the buttons are added to a container. Buttons must be associated with event listeners before the buttons are added to a container. Method actionPerformed() is event handler. Method actionPerformed() is event handler. Kind of object receiving the event is checked. Kind of object receiving the event is checked. Action is performed depending on the kind of object receiving the event. Action is performed depending on the kind of object receiving the event. Method main() simply instantiates the EventDemo class and makes the object visible. Method main() simply instantiates the EventDemo class and makes the object visible.

16 Example: DistanceConverter Program DistanceConverter attaches some calculation steps to event handlers. Its interface consists of two labels, two text fields, and four buttons. Program DistanceConverter attaches some calculation steps to event handlers. Its interface consists of two labels, two text fields, and four buttons. Go to an Applet version of EventDemo

17 Example: DistanceConverter (cont.) Text boxes Text boxes For inputting distances For inputting distances Buttons Buttons “To Meter”—when clicked, converts feet value to meter value and displays in appropriate box “To Meter”—when clicked, converts feet value to meter value and displays in appropriate box "To Feet"—when clicked, converts meters to feet value and displays in appropriate box. "To Feet"—when clicked, converts meters to feet value and displays in appropriate box. "Clear“—clears text fields "Clear“—clears text fields "Instructions“—pops up a message box with a short explanation of the program. "Instructions“—pops up a message box with a short explanation of the program.

18 Example: DistanceConverter (cont.) Here is the source code for DistanceConverter.java Here is the source code for DistanceConverter.javasource code for DistanceConverter.javasource code for DistanceConverter.java Note that: Note that: In constructor DistanceConvert() In constructor DistanceConvert() super() is an invocation to a constructor of the frame's super class. super() is an invocation to a constructor of the frame's super class. setdefaultCloseOperation() calls a method inherited from JFrame setdefaultCloseOperation() calls a method inherited from JFrame Container pane uses a grid layout of 4 rows by 2 columns Container pane uses a grid layout of 4 rows by 2 columns

19 Example: DistanceConverter (cont.) In event handler actionPerformed() In event handler actionPerformed() With interface ActionListener, this is the only method that needs to be implementd. With interface ActionListener, this is the only method that needs to be implementd. After the statement Object src = e.getSource() depending on the identity of src, different actions are performed. After the statement Object src = e.getSource() depending on the identity of src, different actions are performed. To check for valid input, note the use of strFeet.equals(""), and not strFeet == "", because strFeet is a reference, or a pointer. To check for valid input, note the use of strFeet.equals(""), and not strFeet == "", because strFeet is a reference, or a pointer.

20 Example: DistanceConverter (cont.) In the statement In the statement txtMeter.setText(decForm.format(numMeter)+" ") decForm is a formatting object which was instantiated from the DecimalFormat class, at the top of this method. The pattern in its constructor-- "#.00"--specifies two decimal points. Thus, expression decForm.format(numMeter) will return a string of digits with two decimal points.

21 Exercise Recall a simple java program created in the last chapter—GUI--named GUIDemo,java, which consisted of labels for first & last names, textboxes for first & last names, and buttons to start and quit the program. Recall a simple java program created in the last chapter—GUI--named GUIDemo,java, which consisted of labels for first & last names, textboxes for first & last names, and buttons to start and quit the program. Modify the program as follows: Modify the program as follows: Arrange the components using the grid layout manager. (3, 2) Arrange the components using the grid layout manager. (3, 2) Associate an event handler to the start button so that when it is clicked, a message box (showMessageDialog()) outputs a greeting. The greeting is of the form “Welcome to Hawaii, Charles Chang”, where “Charles” and “Chang” are contents of the two text boxes. Associate an event handler to the start button so that when it is clicked, a message box (showMessageDialog()) outputs a greeting. The greeting is of the form “Welcome to Hawaii, Charles Chang”, where “Charles” and “Chang” are contents of the two text boxes.

22 Exercise (cont.) Associate an event with the quit button so that when it is clicked, the program ends. (System.exit(0)) Associate an event with the quit button so that when it is clicked, the program ends. (System.exit(0))

23 Exercise (cont.) GuiDemo.java code


Download ppt "Interactive Programs Java API. Terminology Event—an action or occurrence, not part of a program, detected by the program. Events can be Event—an action."

Similar presentations


Ads by Google