Week 11 - GUI Part II
Week 11 - GUI Part II
GUI (Part 2)
OBJECTIVES
3
EVENT AND EVENT SOURCE OBJECT
4
EVENT CLASSES
▶ You can identify the source object of the event using the
getSource() instance method in the EventObject class.
6
SELECTED USER ACTIONS
Source Event Object Type
User Action Object Generated
7
LISTENERS, REGISTRATIONS,
AND HANDLING EVENTS
8
LISTENERS, REGISTRATIONS,
AND HANDLING EVENTS
▶ Two things are needed for an object to be a listener for an event on
a source object:
1. The listener object’s class must implement the corresponding event-
listener interface & override the corresponding listener method. Java
provides a listener interface for every type of GUI event.
11
LISTENERS, REGISTRATIONS,
AND HANDLING EVENTS
▶ For each event, the source object maintains a list of listeners and
notifies all the registered listeners by invoking the handler on the
listener object to respond to the event.
12
THE DELEGATION MODEL: EXAMPLE
• Repeat the previous steps to rename the variable for Reset button from
jButton2 to resetBtn
resetBtn
STEPS FOR ADDING EVENT
HANDLER FOR THE GUI
1. Renaming the GUI components variables
• Next we are going to rename all the variables for the four textfields
• Right-click the textfield beside label ‘Length:’ and select Change
Variable Name…
STEPS FOR ADDING EVENT
HANDLER FOR THE GUI
1. Renaming the GUI components variables
• In the Rename dialog box, type ‘lengthTF’. (The variable name for the
Length textfield has been changed from jTextField1 to lengthTF)
STEPS FOR ADDING EVENT
HANDLER FOR THE GUI
1. Renaming the GUI components variables
• Repeat the previous steps to rename the remaining three textfields:
- the variable for Width textfield from jTextField2 to widthTF
- the variable for Area textfield from jTextField3 to areaTF
- the variable for Perimeter textfield from jTextField4 to perimeterTF
widthTF
areaTF
perimeterTF
STEPS FOR ADDING EVENT
HANDLER FOR THE GUI
2. Adding the event handlers
Making the Compute Button Work
First, we will make the Compute button works by assigning the
handler to the button:
Specifically, we will use ActionListener responding to
ActionEvent
•Right Click on the Compute button. From the pop-up menu choose Events -->
Action --> actionPerformed.
STEPS FOR ADDING EVENT
HANDLER FOR THE GUI
2. Adding the event handlers
• Netbeans will automatically add an ActionListener to the Compute button
and generate a handler method for handling the listener's actionPerformed
method
• You will be brought into the Source Code window where you implement the
action you want the Compute button to do when the button is pressed. Your
Source Code window should contain the following lines which is the
actionPerformed method that you need to override:
STEPS FOR ADDING EVENT
HANDLER FOR THE GUI
2. Adding the event handlers
• Next, we add the codes to implement what we want the Compute Button to
do (replacing the // TODO add your handling code here: line)
• The action performed by the Compute button can be divided into 3 steps:
1. It is going to accept user inputs, length & width, from the textfields
lengthTF and widthTF and convert the inputs from type String to
double,
2. It will then perform calculation of rectangle area & perimeter based on
the inputs length & width,
3. It will convert the area & perimeter to type String and place them in
the areaTF & perimeterTF respectively.
STEPS FOR ADDING EVENT
HANDLER FOR THE GUI
2. Adding the event handlers
• The finished source code shall look like this:
Step 1
Step 2
Step 3
STEPS FOR ADDING EVENT
HANDLER FOR THE GUI
2. Adding the event handlers
Making the Reset Button Work
•Next, we add the codes to implement what we want the Reset Button to do
(replacing the // TODO add your handling code here: line)
STEPS FOR ADDING EVENT
HANDLER FOR THE GUI
2. Adding the event handlers
• The action performed by the Reset button is to simply erase all texts in the
textfields. To do this, you will set the texts to all of the four textfields to
empty String.
Exit Button
Textfield
• JTextField nameTF = new JTextField(20);
• Data Conversion:
• String to Integer => Integer.parseInt(“2”); “2” 2
• String to double => Double.parseDouble(“2.5”) “2.5” 2.5
• Integer to String => String.valueOf(2) 2 “2”
• double to String => String.valueOf(2.5) 2.5 “2.5”
•
JOptionPane.showMessageDialog(this,"Please input in numeric value for length and width" );
areaTF.setEditable(false); - used to set either the textfileld can be edited (true) or not (false)
Radio Button
String output = "Your selected course is "+course+" and your semester is "+sem;
displayTA.setText(output);