Week15 - GUI Component 1
Week15 - GUI Component 1
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
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
– Berisi method paint untuk drawing Component onscreen
• Class Container
– Kumpulan dari related components
– Berisi method add untuk menambahkan 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
• Label
– Untuk menampilkan teks pada GUI
– Class JLabel
– Dapat menampilkan:
• Saru baris read-only text
• Image
• Text dan image
• Note :
– contoh-contoh program yang ada menggunakan teknik programming secara
manual untuk membuat dan menempatkan tiap komponen
– Anda bisa menggunakan teknik drag and drop tiap komponen dari Toolbox
untuk menampatkan tiap komponen pada window Form, lalu set property tiap
komponen dengan property box (klik kanan pada komponen yang akan diatur
properties nya)
LabelTest.java
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
KeyEvent MouseEvent
MouseEvent
MouseWheelEvent
MouseWheelEvent
• Event-handling model
– Memiliki tiga bagian
• Event source
– GUI component dimana user berinteraksi
• Event object
– Enkapsulasi informasi terkait event yang dilakukan
• Event listener
– Menerima event object ketika ternotifikasi
– Programmer harus melakukan 2 tasks
• Register event listener untuk event source
• Implementasi event-handling method (event handler)
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 dimana user dapat menuliskan/mengisikan
text
• JPasswordField
– Extends dari JTextField
– Dapat menyembukan characters yang dimasukkan user
dengan simbol tertentu yang dimasukkan pada properties
TextFieldTest.j
ava
textField1
textField1 handler
...
13.7 JButton
• Button
– Component berupa user clicks untuk men-trigger aksi
tertentu
– Macam tipe dari Button
• Command buttons
• Check boxes
• Toggle buttons
• Radio buttons
– javax.swing.AbstractButton subclasses
• Command buttons dibuat melalui class JButton
– Generate ActionEvents ketika user meng-clicks button
JComponent
JComponent
AbstractButton
AbstractButton
JButton JToggleButton
JButton JToggleButton
JCheckBox JRadioButton
JCheckBox JRadioButton
• State buttons
– On/Off or true/false values
– Java menyediakan tiga tipe stae button
• JToggleButton
• JCheckBox
• JRadioButton
CheckBoxTest.ja
va
13.9 JComboBox
• JComboBox
– Berupa List of items dimana user dapat memilih
– Disebut juga drop-down list
13.10 JList
• List
– Urutan items
– user dapat memilih satu atau lebih item
– Single-selection vs. multiple-selection
– JList
• Multiple-selection list
– Memilih beberapa items dari 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
Relocate Person
Person