Java Programming: From
Problem Analysis to Program
Design, 5e
Graphical User Interface
Event Handling
Graphical User Interface (GUI) Components
• View inputs and outputs simultaneously
• One graphical window
• Input values in any order
• Change input values in window
• Click on buttons to get output
Java Programming: From Problem Analysis to
2
Program Design, 5e
Java GUI Components
Java Programming: From Problem Analysis to
3
Program Design, 5e
Graphical User Interface (GUI) Components
(continued)
• GUI components placed in content pane
• GUI components
• Windows
• Labels
• Text areas
• Buttons
Java Programming: From Problem Analysis to
4
Program Design, 5e
GUI Components
• Added to content pane of window
• Not added to window itself
• Pixel: picture element
Java Programming: From Problem Analysis to
5
Program Design, 5e
Windows
• Can be created using a JFrame object
• The class JFrame provides various methods to control attributes of a
window
• Measured in pixels of height and width
• Attributes associated with windows
• Title
• Width
• Height
Java Programming: From Problem Analysis to
6
Program Design, 5e
class JFrame
• GUI window instance created as instance of JFrame
• Provides various methods to control window attributes
Java Programming: From Problem Analysis to
7
Program Design, 5e
Methods Provided by the class JFrame
Java Programming: From Problem Analysis to
8
Program Design, 5e
Methods Provided by the class Jframe (continued)
Java Programming: From Problem Analysis to
9
Program Design, 5e
Two Ways to Create a Window
• First way
• Declare object of type JFrame
• Instantiate object
• Use various methods to manipulate window
• Second way
• Create class containing application program by extending definition of class
JFrame
• Utilizes mechanism of inheritance
Java Programming: From Problem Analysis to
10
Program Design, 5e
Content Pane
• Inner area of GUI window (below title bar, inside
border)
• To access content pane:
• Declare reference variable of type Container
• Use method getContentPane of class JFrame
Java Programming: From Problem Analysis to
11
Program Design, 5e
Methods Provided by the class Container
Java Programming: From Problem Analysis to
12
Program Design, 5e
class JLabel
• Labels: objects of particular class type
• class JLabel: used to create labels
• Label attributes
• Title
• Width
• Height
• To create a label:
• Instantiate object of type JLabel
• Modify attributes to control display of labels
Java Programming: From Problem Analysis to
13
Program Design, 5e
class Jlabel (continued)
14
Java Programming: From Problem Analysis to Program Design, 5e
class JTextField
• Text fields: objects belonging to class JTextField
• To create text field:
• Declare reference variable of type JTextField
• Instantiate object
Java Programming: From Problem Analysis to
15
Program Design, 5e
class JTextField (continued)
Java Programming: From Problem Analysis to
16
Program Design, 5e
class JTextField (continued)
Java Programming: From Problem Analysis to
17
Program Design, 5e
class JButton
• Provided to create buttons in Java
• To create button:
• Same technique as creating JLabel and JTextField
Java Programming: From Problem Analysis to
18
Program Design, 5e
class Jbutton (continued)
Java Programming: From Problem Analysis to
19
Program Design, 5e
Handling an Event
• Action event: event created when JButton is
clicked
• Event listener: object that receives message when
JButton is clicked
• In Java, you must register the listener
Java Programming: From Problem Analysis to
20
Program Design, 5e
Handling an Event (continued)
• class ActionListener
• Handles action event
• Part of package java.awt.Event
• The class ActionListener is a special type of class (interface)
• Must contain actionPerformed method
Java Programming: From Problem Analysis to
21
Program Design, 5e
Rectangle Program: Sample Run
Java Programming: From Problem Analysis to
22
Program Design, 5e
Programming Example: Temperature
Conversion
• Input: temperature in Fahrenheit or Celsius
• Output: temperature in Celsius if input is Fahrenheit; temperature in
Fahrenheit if input is Celsius
Java Programming: From Problem Analysis to
23
Program Design, 5e
Programming Example: Temperature
Conversion (continued)
• Solution
• Create the appropriate JLabels, JTextFields, JButtons
• Add them to the created content pane
• Calculate the appropriate conversions when the buttons are clicked and an
event is triggered
Java Programming: From Problem Analysis to
24
Program Design, 5e
Sample Run for TempConversion
Java Programming: From Problem Analysis to
25
Program Design, 5e
Quick Quiz
1. True or False: The method setTitle is part of the class
JFrame.
2. GUI components are placed in an area of the GUI window
called the __________.
3. True or False: The class Container is part of the package
javax.swing.
4. A subclass inherits all the properties of the __________.
5. When a button in the GUI is clicked, a(n) __________ has
occurred.
1. True
2. content pane
3. False
4. superclass
5. event
Event Handling
• Action events
• Handled by implementing interface ActionListener
• Window events
• Handled by implementing interface WindowListener
• Mouse events
• Handled by implementing interface MouseListener
• Key events
• Handled by implementing interface KeyListener
Java Programming: From Problem Analysis to
28
Program Design, 5e
Event Handling (continued)
• class WindowAdapter
• Implements interface WindowListener with empty bodies to
methods
• class MouseAdapter
• Implements interface MouseListener with empty bodies to methods
Java Programming: From Problem Analysis to
29
Program Design, 5e
Registering Listeners
• Registering window listener object to GUI component
• Use method addWindowListener
• Window listener object being registered is passed as parameter
to method addWindowListener
• Registering mouse listener object to GUI component
• Use method addMouseListener
• Mouse listener object being registered is passed as parameter to
method addMouseListener
Java Programming: From Problem Analysis to
30
Program Design, 5e
Event Handling (continued)
Java Programming: From Problem Analysis to
31
Program Design, 5e
Event Handling (continued)
Java Programming: From Problem Analysis to
32
Program Design, 5e
Event Handling (continued)
Java Programming: From Problem Analysis to
33
Program Design, 5e
Programming Example: Calculator
Java Programming: From Problem Analysis to
34
Program Design, 5e