Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tonga Institute of Higher Education

Similar presentations


Presentation on theme: "Tonga Institute of Higher Education"— Presentation transcript:

1 Tonga Institute of Higher Education
Event Handling Tonga Institute of Higher Education

2 Event Driven Programs Event Driven Programs – The program responds to a user event Event - An action or occurrence detected by a program User Clicking a mouse button Pressing a key System Running out of memory An error occurring For example, your program might work like this: Show a form with a welcome message. The form will also contain a textbox, OK button and instructions to enter their name and click the Ok button. Wait for the user to respond When the user performs an action, If the user clicks the X in the top right corner, quit the program If the user typed in their name and pressed the OK button, show a message that you are happy to meet them Gives more freedom to the user. It reacts to the user’s actions Gives user the impression that they are in control Frames are event driven!

3 Current Situation We learned how to display a graphical user interface
However, it doesn’t do anything Event – Something that is triggered by the user of a GUI Listener – Code that can detect and respond to events Register – To associate a listener with a GUI object

4 Button Listener Class Need to import packages Need to make a class
To handle events. Don’t forget to implement ActionListener! Handles 2 buttons This is the text of the button that will be handled The only code you should change has a yellow background

5 Setting Up Button Listeners
Declare and Initialize the Listeners Same listener Must use this method Register the Listeners (Connect the listener to the button)

6 Demonstration GUI

7 Button Listener Class that Accepts a Frame
This listener keeps track of the frame so it can change the frame. Pass in the frame through the constructor This changes the text for the button on the frame. The only code you should change has a yellow background

8 Setting Up Listeners that Passes a Frame
Declare and Initialize the Listeners Pass this frame in the constructor Register the Listeners (Connect the listener to the button)

9 Demonstration GUI2

10 Frame Listener Class Only change code that is inside of the brackets
Everything else should be the same!

11 Setting up a Form Listener
This is the constructor Use the word this to add a listener Same listener

12 Demonstration GUI3


Download ppt "Tonga Institute of Higher Education"

Similar presentations


Ads by Google