SlideShare a Scribd company logo
Creating Graphical User Interface
(GUI & Swing, Part 1)
References:
(1)Textbook, chapter 10, 11
(2)Java-Tutorials/tutorial-2015/uiswing/index.html
Java GUI and the swing package
Why should we study this lecture?
Do you want to create GUI applications (graphic
user interface)?
How do GUI applications execute?
What do Java APIs support for developing GUI
App?
2
Objectives
Review
Knowledge used in this lecture:
Event: Some thing happens.
Event in applications: In almost cases, when IO
devices complete a task, an event is thrown  When
user presses a key or clicks mouse, events are
thrown.
Event handler: Code executes when the appropriate
event is thrown.
How does application know an event?
When an IO device completes a task, a signal (called
as interrupt) from the device will be sent to CPU and
operating system (OS) knows it. OS collects states of
keyboard, mouse position, mouse keys,…, called as
event object then OS passes the event object to the
application.
3
Contents
Two Kinds of Applications
Java Model for Event Management
About Java Foundation Classes (JFC)
A Strategy for Designing the GUI
Using Ordinary Swing Components
A Demonstration
4
1- Two Kinds of Apps
Console App.
Compute-centric Apps
Event-based App.
User-centric Apps(GUI)
Model-View Controller Architecture for GUI component.
Model: Object contains data.
View: Object users can see it on the screen
Controller: Object manages events
[www.songho.ca]5
2-Java model for event
management
Delegates
View
Register Table
Update
GUI
6
Component C1 Specific Event Object E1
- Component
- Keyboard state
- Mouse state
Listener L1
Event handler 1
Event handler 2
Model
-data
-Reference to
listener object Run time Environment (JVM)
Event Component Listener code
E1 C1 L1 1234
E2 C2 L2 5678
……..
Application
(from OS)
Controller
Some Common Events
Object Event Interface Method
JButton ActionEvent ActionListener actionPerformed()
JCheckBox ActionEvent
ItemEvent
ActionListener
ItemListener
actionPerformed()
itemStateChanged()
JRadioButton ActionEvent
ItemEvent
ActionListener
ItemListener
actionPerformed()
itemStateChanged()
JTextField
JTextArea
ActionEvent
FocusEvent
ActionListener
FocusListener
actionPerformed()
focusGained(), focusLost()
JPasswordField ActionEvent ActionListener actionPerformed()
7
More Details about events will be introduced in the next session.
3- About Java Foundation Classes
8
 JFC encompass a group of features for building graphical user
interfaces (GUIs) and adding rich graphics functionality and interactivity
to Java applications. It is defined as containing the features shown in the
table below:
This trail concentrates on the Swing components. We help you choose the appropriate components for your GUI, tell you how to use them, and give you the background
Feature Description
Swing GUI
Components
Includes everything from buttons to split panes to tables. Many components are capable of sorting,
printing, and drag and drop, to name a few of the supported features.
Pluggable Look-
and-Feel Support
Allowing a choice of look and feel. For example, the same program can use either the Java or the
Windows look and feel. Additionally, the Java platform supports the GTK+ look and feel, which
makes hundreds of existing look and feels available to Swing programs. Many more look-and-feel
packages are available from various sources.
Accessibility API Enables assistive technologies, such as screen readers and Braille displays, to get information from
the user interface.
Java 2D API Enables developers to easily incorporate high-quality 2D graphics, text, and images in applications
and applets. Java 2D includes extensive APIs for generating and sending high-quality output to
printing devices.
Internationalization Allows developers to build applications that can interact with users worldwide in their own
languages and cultural conventions. With the input method framework developers can build
applications that accept text in languages that use thousands of different characters, such as
Japanese, Chinese, or Korean.
JFC…
9
 Which Swing Packages Should I Use?
The Swing API is powerful, flexible — and immense. The Swing API has 18
public packages:
This trail concentrates on the Swing components. We help you choose the appropriate components for your GUI, tell you how to use them, and give you the background
4- A Strategy for Designing the GUI
Identify needed components.
Isolate regions and behaviors.
Sketch (phác hoạ) the GUI.
Choose Layout managers.
Requirements
Input data
Output Data
Operations
Implement
event handlers
10
5- Ordinary Swing Component
11
Component Properties:
Name, size, location,
background color,
foreground color, font,
enable, editable
Set/get
Ordinary Swing Components
javax.swing package contains APIs for developing
GUI Apps Refer to Java documentation for more
details of this package.
Almost component class names begin with ‘J’
Three categories:
Container components
Ordinary components:
Menu components.
12
Ordinary Swing Components…
Containers: JFrame, JPanel
They contain other components.
They use layout managers to
determine the size and position of
their child components.
JFrame is a top-level container.
JPanel is called general-purpose
container. It is a lightweight container.
13
Ordinary Components
JLabel
JTextfield
JRadioButton
(ButtonGroup)
JComboBox
JTextArea
JScrollBar
JCheckBox
JButton
14
Demo 1
15
Objectives
Use a panel to group some components
Hide and show those as a whole through this panel
Disable the exit button on the frame
The exit button allows user close the application
GUI:
Demo 1
16
Design
Demo 1
17
Class Object name Properties
JFrame GUI_01 Layout: null, defaultCloseOperation: DO_NOTHING
Title: GUI-Demo 1
JPanel pEmp Layout: null, Border: tittleBorder
JLabel lbCode Opaque: true, background: green, foreground: white
horizontalAlignment: RIGHT, text: Code
JLabel others Text: Name, Sex, Degree
ButtonGroup bgrpSex
JRadioButton rdMale buttonGroup: bgrpSex, Text: Male, Selected: true,
JRadioButton rdFemale buttonGroup: bgrpSex, Text: Female, Selected: false
JComboBox cbDegree Model:Ph. D., Master, Engineer, Bachelor, Technician
Setup Properties
Demo 1
18
Code:Constructor
Code for handling the event on the button Exit
Code for handling the event on the button ShowHide
Demo 1
19
View code at the end of the source file, inserted
by NetBeans  Comment
View the contents of the method
initComponents(), inserted by NetBeans, to
explore anonymous classes, then verify them
using Window Explorer
Summary
Two Kinds of Applications
Java Model for Event Management
About Java Foundation Classes (JFC)
A Strategy for Designing the GUI
Using Basic Swing Components
20

More Related Content

PDF
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
PDF
Android - From Zero to Hero @ DEVit 2017
PPTX
Android apps development
PPTX
Android application-component
PDF
Ui layout (incomplete)
PDF
Ch12. graphical user interfaces
PDF
Android UI Fundamentals part 1
PDF
Android development basics
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
Android - From Zero to Hero @ DEVit 2017
Android apps development
Android application-component
Ui layout (incomplete)
Ch12. graphical user interfaces
Android UI Fundamentals part 1
Android development basics

What's hot (20)

PPT
android-tutorial-for-beginner
PDF
PPTX
PPT
Google Android
PDF
Android Basic Components
PDF
AndroidManifest
PPTX
Android components
PDF
Android Development in a Nutshell
PPT
Android In A Nutshell
ODP
Ppt 2 android_basics
PPT
11 Java User Interface Libraries for Developing Mobile Applications
PDF
Android Basics
PPT
Android Applications Development
PPTX
GDG School Android Workshop
PPTX
Android architecture
PPTX
Android programming basics
PDF
Android Development: Build Android App from Scratch
PDF
Android session 2
PPTX
Android basic principles
android-tutorial-for-beginner
Google Android
Android Basic Components
AndroidManifest
Android components
Android Development in a Nutshell
Android In A Nutshell
Ppt 2 android_basics
11 Java User Interface Libraries for Developing Mobile Applications
Android Basics
Android Applications Development
GDG School Android Workshop
Android architecture
Android programming basics
Android Development: Build Android App from Scratch
Android session 2
Android basic principles
Ad

Similar to Slot04 creating gui (20)

PPT
14a-gui.ppt
PPTX
Chapter 1 swings
PDF
28-GUI application.pptx.pdf
PDF
Z blue introduction to gui (39023299)
PPT
Chap1 1 1
PPT
Chap1 1.1
PPT
L11cs2110sp13
PPTX
SWING USING JAVA WITH VARIOUS COMPONENTS
PDF
GUI.pdf
PPTX
Chapter 11.1
PPTX
Advanced Java GUI Programming_ AWT, Swing, and Event Handling_AI PPT Maker.pptx
PPT
Java lecture
PPTX
01. introduction to swing
PPT
Graphic User Interface (GUI) Presentation
PPT
Chapter One Java Swings GUA programming.ppt
PDF
JAVA GUI PART I
PDF
Ebook Pdf O Reilly Java Swing
PPT
03_GUI.ppt
PPT
Graphical User Interface (GUI) - 1
14a-gui.ppt
Chapter 1 swings
28-GUI application.pptx.pdf
Z blue introduction to gui (39023299)
Chap1 1 1
Chap1 1.1
L11cs2110sp13
SWING USING JAVA WITH VARIOUS COMPONENTS
GUI.pdf
Chapter 11.1
Advanced Java GUI Programming_ AWT, Swing, and Event Handling_AI PPT Maker.pptx
Java lecture
01. introduction to swing
Graphic User Interface (GUI) Presentation
Chapter One Java Swings GUA programming.ppt
JAVA GUI PART I
Ebook Pdf O Reilly Java Swing
03_GUI.ppt
Graphical User Interface (GUI) - 1
Ad

Recently uploaded (20)

PDF
Classroom Observation Tools for Teachers
PDF
Yogi Goddess Pres Conference Studio Updates
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Paper A Mock Exam 9_ Attempt review.pdf.
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
RMMM.pdf make it easy to upload and study
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
Classroom Observation Tools for Teachers
Yogi Goddess Pres Conference Studio Updates
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Paper A Mock Exam 9_ Attempt review.pdf.
Orientation - ARALprogram of Deped to the Parents.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Anesthesia in Laparoscopic Surgery in India
RMMM.pdf make it easy to upload and study
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Complications of Minimal Access Surgery at WLH
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Chinmaya Tiranga quiz Grand Finale.pdf
Cell Types and Its function , kingdom of life
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Final Presentation General Medicine 03-08-2024.pptx

Slot04 creating gui

  • 1. Creating Graphical User Interface (GUI & Swing, Part 1) References: (1)Textbook, chapter 10, 11 (2)Java-Tutorials/tutorial-2015/uiswing/index.html Java GUI and the swing package
  • 2. Why should we study this lecture? Do you want to create GUI applications (graphic user interface)? How do GUI applications execute? What do Java APIs support for developing GUI App? 2 Objectives
  • 3. Review Knowledge used in this lecture: Event: Some thing happens. Event in applications: In almost cases, when IO devices complete a task, an event is thrown  When user presses a key or clicks mouse, events are thrown. Event handler: Code executes when the appropriate event is thrown. How does application know an event? When an IO device completes a task, a signal (called as interrupt) from the device will be sent to CPU and operating system (OS) knows it. OS collects states of keyboard, mouse position, mouse keys,…, called as event object then OS passes the event object to the application. 3
  • 4. Contents Two Kinds of Applications Java Model for Event Management About Java Foundation Classes (JFC) A Strategy for Designing the GUI Using Ordinary Swing Components A Demonstration 4
  • 5. 1- Two Kinds of Apps Console App. Compute-centric Apps Event-based App. User-centric Apps(GUI) Model-View Controller Architecture for GUI component. Model: Object contains data. View: Object users can see it on the screen Controller: Object manages events [www.songho.ca]5
  • 6. 2-Java model for event management Delegates View Register Table Update GUI 6 Component C1 Specific Event Object E1 - Component - Keyboard state - Mouse state Listener L1 Event handler 1 Event handler 2 Model -data -Reference to listener object Run time Environment (JVM) Event Component Listener code E1 C1 L1 1234 E2 C2 L2 5678 …….. Application (from OS) Controller
  • 7. Some Common Events Object Event Interface Method JButton ActionEvent ActionListener actionPerformed() JCheckBox ActionEvent ItemEvent ActionListener ItemListener actionPerformed() itemStateChanged() JRadioButton ActionEvent ItemEvent ActionListener ItemListener actionPerformed() itemStateChanged() JTextField JTextArea ActionEvent FocusEvent ActionListener FocusListener actionPerformed() focusGained(), focusLost() JPasswordField ActionEvent ActionListener actionPerformed() 7 More Details about events will be introduced in the next session.
  • 8. 3- About Java Foundation Classes 8  JFC encompass a group of features for building graphical user interfaces (GUIs) and adding rich graphics functionality and interactivity to Java applications. It is defined as containing the features shown in the table below: This trail concentrates on the Swing components. We help you choose the appropriate components for your GUI, tell you how to use them, and give you the background Feature Description Swing GUI Components Includes everything from buttons to split panes to tables. Many components are capable of sorting, printing, and drag and drop, to name a few of the supported features. Pluggable Look- and-Feel Support Allowing a choice of look and feel. For example, the same program can use either the Java or the Windows look and feel. Additionally, the Java platform supports the GTK+ look and feel, which makes hundreds of existing look and feels available to Swing programs. Many more look-and-feel packages are available from various sources. Accessibility API Enables assistive technologies, such as screen readers and Braille displays, to get information from the user interface. Java 2D API Enables developers to easily incorporate high-quality 2D graphics, text, and images in applications and applets. Java 2D includes extensive APIs for generating and sending high-quality output to printing devices. Internationalization Allows developers to build applications that can interact with users worldwide in their own languages and cultural conventions. With the input method framework developers can build applications that accept text in languages that use thousands of different characters, such as Japanese, Chinese, or Korean.
  • 9. JFC… 9  Which Swing Packages Should I Use? The Swing API is powerful, flexible — and immense. The Swing API has 18 public packages: This trail concentrates on the Swing components. We help you choose the appropriate components for your GUI, tell you how to use them, and give you the background
  • 10. 4- A Strategy for Designing the GUI Identify needed components. Isolate regions and behaviors. Sketch (phác hoạ) the GUI. Choose Layout managers. Requirements Input data Output Data Operations Implement event handlers 10
  • 11. 5- Ordinary Swing Component 11 Component Properties: Name, size, location, background color, foreground color, font, enable, editable Set/get
  • 12. Ordinary Swing Components javax.swing package contains APIs for developing GUI Apps Refer to Java documentation for more details of this package. Almost component class names begin with ‘J’ Three categories: Container components Ordinary components: Menu components. 12
  • 13. Ordinary Swing Components… Containers: JFrame, JPanel They contain other components. They use layout managers to determine the size and position of their child components. JFrame is a top-level container. JPanel is called general-purpose container. It is a lightweight container. 13
  • 15. Demo 1 15 Objectives Use a panel to group some components Hide and show those as a whole through this panel Disable the exit button on the frame The exit button allows user close the application GUI:
  • 17. Demo 1 17 Class Object name Properties JFrame GUI_01 Layout: null, defaultCloseOperation: DO_NOTHING Title: GUI-Demo 1 JPanel pEmp Layout: null, Border: tittleBorder JLabel lbCode Opaque: true, background: green, foreground: white horizontalAlignment: RIGHT, text: Code JLabel others Text: Name, Sex, Degree ButtonGroup bgrpSex JRadioButton rdMale buttonGroup: bgrpSex, Text: Male, Selected: true, JRadioButton rdFemale buttonGroup: bgrpSex, Text: Female, Selected: false JComboBox cbDegree Model:Ph. D., Master, Engineer, Bachelor, Technician Setup Properties
  • 18. Demo 1 18 Code:Constructor Code for handling the event on the button Exit Code for handling the event on the button ShowHide
  • 19. Demo 1 19 View code at the end of the source file, inserted by NetBeans  Comment View the contents of the method initComponents(), inserted by NetBeans, to explore anonymous classes, then verify them using Window Explorer
  • 20. Summary Two Kinds of Applications Java Model for Event Management About Java Foundation Classes (JFC) A Strategy for Designing the GUI Using Basic Swing Components 20