Jlabel: 2003 Prentice Hall, Inc. All Rights Reserved
Jlabel: 2003 Prentice Hall, Inc. All Rights Reserved
Jlabel: 2003 Prentice Hall, Inc. All Rights Reserved
Components: Part 1
Outline
13.1 Introduction
13.2 Overview of Swing Components
13.3 JLabel
13.4 Event Handling
13.5 TextFields
13.6 How Event Handling Works
13.7 JButton
13.8 JCheckBox and JRadioButton
13.9 JComboBox
13.10 JList
13.11 Multiple-Selection Lists
13.12 Mouse Event Handling
13.13 Adapter Classes
13.14 Key Event Handling
Components: Part 1
Outline
13.15 Layout Managers
13.15.1 FlowLayout
13.15.2 BorderLayout
13.15.3 GridLayout
13.16 Panels
13.17 (Optional Case Study) Thinking About Objects: Use Cases
13.1 Introduction
components
scroll bars
Component Description
JLabel An area where uneditable text or icons can be displayed.
JTextField An area in which the user inputs data from the keyboard. The area can also
display information.
JButton An area that triggers an event when clicked with the mouse.
JCheckBox A GUI component that is either selected or not selected.
JComboBox A drop-down list of items from which the user can make a selection by
clicking an item in the list or possibly by typing into the box.
JList An area containing a list of items from which the user can make a selection
by clicking on any element in the list. Multiple elements can be selected.
JPanel A container in which components can be placed and organized.
• Class Component
– Contains method paint for drawing Component onscreen
• Class Container
– Collection of related components
– Contains method add for adding components
• Class JComponent
– Pluggable look and feel for customizing look and feel
– Shortcut keys (mnemonics)
– Common event-handling capabilities
Object
Object
Component
Component
Container
Container
JComponent
JComponent
13.3 JLabel
• Label
– Provide text on GUI
– Defined with class JLabel
– Can display:
• Single line of read-only text
• Image
• Text and image
LabelTest.java
java.awt.event
Object
Object
ActionEvent
ActionEvent
EventObject
EventObject
AdjustmentEvent
AdjustmentEvent
AWTEvent
AWTEvent ContainerEvent
ContainerEvent
ItemEvent
ItemEvent
FocusEvent
FocusEvent
TextEvent
TextEvent
PaintEvent
PaintEvent
ComponentEvent
ComponentEvent
WindowEvent
WindowEvent
InputEvent
InputEvent
KeyEvent MouseEvent
KeyEvent MouseEvent
MouseWheelEvent
MouseWheelEvent
• Event-handling model
– Three parts
• Event source
– GUI component with which user interacts
• Event object
– Encapsulates information about event that occurred
• Event listener
– Receives event object when notified, then responds
– Programmer must perform two tasks
• Register event listener for event source
• Implement event-handling method (event handler)
java.awt.event
interface
«interface»
ActionListener
ActionListener
interface
«interface»
AdjustmentListener
AdjustmentListener
interface
«interface»
ComponentListener
ComponentListener
interface
«interface»
ContainerListener
ContainerListener
interface
«interface»
FocusListener
FocusListener
interface
«interface» «interface»
interface
EventListener ItemListener
EventListener ItemListener
interface
«interface»
KeyListener
KeyListener
interface
«interface»
MouseListener
MouseListener
interface
«interface»
MouseMotionListener
MouseMotionListener
interface
«interface»
TextListener
TextListener
interface
«interface»
WindowListener
TextListener
13.5 TextFields
• JTextField
– Single-line area in which user can enter text
• JPasswordField
– Extends JTextField
– Hides characters that user enters
TextFieldTest.j
ava
textField1
textField1 handler
...
13.7 JButton
• Button
– Component user clicks to trigger a specific action
– Several different types
• Command buttons
• Check boxes
• Toggle buttons
• Radio buttons
– javax.swing.AbstractButton subclasses
• Command buttons are created with class JButton
– Generate ActionEvents when user clicks button
JComponent
JComponent
AbstractButton
AbstractButton
JButton JToggleButton
JButton JToggleButton
JCheckBox JRadioButton
JCheckBox JRadioButton
• State buttons
– On/Off or true/false values
– Java provides three types
• JToggleButton
• JCheckBox
• JRadioButton
CheckBoxTest.ja
va
13.9 JComboBox
• JComboBox
– List of items from which user can select
– Also called a drop-down list
13.10 JList
• List
– Series of items
– user can select one or more items
– Single-selection vs. multiple-selection
– JList
• Multiple-selection list
– Select many items from Jlist
– Allows continuous range selection
• Adapter class
– Implements interface
– Provides default implementation of each interface method
– Used when all methods in interface is not needed
ContainerAdapter ContainerListener
FocusAdapter FocusListener
KeyAdapter KeyListener
MouseAdapter MouseListener
MouseMotionAdapter MouseMotionListener
WindowAdapter WindowListener
isAltDown() Returns true when the user clicks the middle mouse button on a
mouse with three buttons. To simulate a middle-mouse-button click
on a one- or two-button mouse, the user can press the Alt key on the
keyboard and click the only- or left-mouse button, respectively.
• Interface KeyListener
– Handles key events
• Generated when keys on keyboard are pressed and released
• KeyEvent
– Contains virtual key code that represents key
KeyDemo.java
• Layout managers
– Provided for arranging GUI components
– Provide basic layout capabilities
– Processes layout details
– Programmer can concentrate on basic “look and feel”
– Interface LayoutManager
BorderLayout Default for the content panes of JFrames (and other windows) and
JApplets. Arranges the components into five areas: NORTH, SOUTH,
EAST, WEST and CENTER.
GridLayout Arranges the components into rows and columns.
13.15.1 FlowLayout
• FlowLayout
– Most basic layout manager
– GUI components placed in container from left to right
13.15.2 BorderLayout
• BorderLayout
– Arranges components into five regions
• NORTH (top of container)
• SOUTH (bottom of container)
• EAST (left of container)
• WEST (right of container)
• CENTER (center of container)
BorderLayoutDem
o.java
13.15.3 GridLayout
• GridLayout
– Divides container into grid of specified row an columns
– Components are added starting at top-left cell
• Proceed left-to-fight until row is full
13.16 Panels
• Panel
– Helps organize components
– Class JPanel is JComponent subclass
– May have components (and other panels) added to them
Create Person
User
perspective of a Person
Relocate Person
Person