SlideShare a Scribd company logo
CIS 068
Welcome to CIS 083 !
Introduction to GUIs:
JAVA Swing
CIS 068
Overview
• JAVA and GUIs: SWING
– Container, Components, Layouts
– Using SWING
CIS 068
The First Swing Program
Example:
The First Swing Program
CIS 068
The GUI
Container: JFrame
Layout: BorderLayout
North
Center
Components: JLabel JButton, containing
an ImageIcon
CIS 068
Steps to build a GUI
1. import package
2. set up top level container
(e.g. JFrame)
3. apply layout
(e.g. BorderLayout)
4. add components
(e.g. Label, Button)
5. REGISTER listeners
6. show it to the world !
CIS 068
The Source
. import package
2. set up top level container
(e.g. JFrame)
3. apply layout
(e.g. BorderLayout)
4. add components
(e.g. Label, Button)
5. REGISTER listeners
6. show it to the world !
CIS 068
Swing Components
• Top Level Containers
• General Purpose Containers
• Special Purpose Containers
• Basic Controls
• Uneditable Information Displays
• Interactive Displays of Highly Formatted
Information
CIS 068
Swing Components
Top Level Containers
Your application usually extends one of these classes !
CIS 068
Swing Components
General Purpose Containers
CIS 068
Swing Components
General Purpose Containers
• typically used to collect Basic Controls
(JButton, JChoiceBox…)
• Added to layout of top-level containers
JPanel
JFrame
CIS 068
Swing Components
Special Purpose Containers
CIS 068
Swing Components
Special Purpose Containers
• If you want to use them, go to
java.sun.com
CIS 068
Swing Components
Basic Controls
CIS 068
Swing Components
Basic Controls
• Unlike ‘passive’ containers, controls are
the ‘active’ part of your GUI
Remark: containers aren’t only ‘passive’, they are also ‘active’ sources of events,
eg. Mouse-events.
• Being the visible part of your interface,
controls bring your application to life
• Controls are event sources !
• Objects of your application register to
controls to handle the events
CIS 068
Swing Components
Uneditable Information Displays
CIS 068
Swing Components
Interactive Displays of Highly Formatted
Information
CIS 068
Swing Components
Interactive Displays of Highly Formatted
Information
• Define standard interfaces for
frequently needed tasks
... go to java.sun.com for further
information ...
CIS 068
Layout Management
How to glue it all together:
The Layout Management
CIS 068
Layout Management
• The process of determining the size and
position of components
• A layout manager is an object that performs
layout management for the components
within the container.
• Layout managers have the final say on the
size and position of components added to a
container
• Using the add method to put a component in
a container, you must ALWAYS take the
container's layout manager into account
CIS 068
Layout Management
... and finally, the layout manager
preserves the world from home
made layout-design !
CIS 068
Layout Management
Java supplies five commonly used layout
managers:
1. BorderLayout
2. BoxLayout
3. FlowLayout
4. GridBagLayout
5. GridLayout
CIS 068
Layouts
BorderLayout
Position must be specified, e.g. add (“North”, myComponent)
CIS 068
Layouts
BoxLayout
The BoxLayout class puts
components in a single row
or column.
It respects the components‘
requested maximum sizes.
CIS 068
Layouts
FlowLayout
FlowLayout is the default layout manager for every JPanel.
It simply lays out components from left to right, starting new
rows if necessary
CIS 068
Layouts
GridBagLayout
GridBagLayout is the most sophisticated, flexible layout manager the
Java platform provides. If you really want to use it, go to java.sun.com …
CIS 068
Layouts
GridLayout
GridLayout simply makes a bunch of components equal in size and
displays them in the requested number of rows and columns .
CIS 068
Using Components
Examples:
• Using a JButton
• Using a JSlider
• Using a JCheckBox
CIS 068
Using a JButton
Some Constructors:
JButton() Creates a button with no text or icon
JButton(Icon icon) Creates a button with an icon
JButton(String text) Creates a button with text
JButton(String text,
Icon icon)
Creates a button with initial text and
an icon
CIS 068
Using a JButton
Some Methods:
addActionListener(
ActionListener a)
Registers ActionListener to JButton
Inherited from AbstractButton
setFont(Font font) Specifies Font (Type, Style, Size)
Inherited from JComponent
setBackground(
Color color)
Sets background color
Inherited from JComponent
setActionCommand
(String text)
Used to specify button if listener is
registered to multiple buttons (see
ActionEvent.getActionCommand())
CIS 068
Using a JSlider
Some Constructors:
JSlider() Creates a horizontal slider with the
range 0 to 100 and an initial value of 50
JSlider(
int min, int max,
int value)
Creates a horizontal slider using the
specified min, max and value.
JSlider(
Int orientation
int min, int max,
int value)
Creates a slider with the specified
orientation and the specified minimum,
maximum, and initial values.
CIS 068
Using a JSlider
Some Methods:
addChangeListener
(ChangeListener cl)
Registers ChangeListener to slider
int getValue() Returns the slider’s value
setValue(int value) Sets the slider’s value
CIS 068
Using a JCheckBox
Some Constructors:
JCheckBox() Creates an initially unselected check box
button with no text, no icon.
JCheckBox(
String text)
Creates an initially unselected check box
with text.
JCheckBox(
String text,
Icon icon,
boolean selecte
d)
Creates a check box with text and icon,
and specifies whether or not it is initially
selected.
CIS 068
Using a JCheckBox
Some Methods:
addItemListener
(ItemListener il)
Registers ItemListener to checkbox
Inherited from AbstractButton
setSelected(
boolean select)
Sets the state of checkbox
Inherited from AbstractButton
boolean
getSeleted()
Gets the state of checkbox.
calling method often saves from registering to the
checkbox !
CIS 068
Custom Painting
creating your own graphics:
Custom Painting
CIS 068
Custom Painting
Decide which superclass to use, for example:
• JPanel: Generating and displaying graphs in top of a
blank or transparent background
• JLabel: Painting on top of an image
• JButton: custom button
• …
Every class derived from JComponent can be used
for custom drawing !
(Recommended: JPanel)
CIS 068
Custom Painting
The Graphics Object
• provides both a context for painting and
methods for performing the painting.
• Example of methods
– drawImage
– drawString
– drawRect
– fillRect
– setColor
– …
• passed as argument to the
paintComponent - method
CIS 068
Custom Painting
The paintComponent method
• Method of class JComponent
• Inherited to all subclasses, e.g. JPanel,
JButton,…
• The place where all custom painting
belongs !
• Invoked by the event-scheduler or by the
repaint() - method
CIS 068
Using Swing
(demo program)
CIS 068
At last...
This was a BRIEF overview and introduction to SWING.
SWING has MUCH more to offer, see
• http://java.sun.com/docs/books/tutorial/uiswing/
• http://java.sun.com/j2se/1.4.1/docs/api/

More Related Content

Similar to Swing demo presentation for the swing java (20)

PPT
GUI Programming In Java
yht4ever
 
PPT
java swing
Waheed Warraich
 
PPT
Swing and AWT in java
Adil Mehmoood
 
PDF
Swingpre 150616004959-lva1-app6892
renuka gavli
 
PPTX
swing_compo.pptxsfdsfffdfdfdfdgwrwrwwtry
zmulani8
 
PDF
Lecture 6.pdf
SakhilejasonMsibi
 
PPT
Graphical User Interface in JAVA
suraj pandey
 
PPT
Swing and Graphical User Interface in Java
babak danyal
 
PPTX
tL19 awt
teach4uin
 
PPTX
Graphical User Interface (GUI)
SAGARDAVE29
 
PDF
swingbasics
Arjun Shanka
 
PDF
JEDI Slides-Intro2-Chapter19-Abstract Windowing Toolkit and Swing.pdf
MarlouFelixIIICunana
 
PPT
03_GUI.ppt
DrDGayathriDevi
 
PDF
28-GUI application.pptx.pdf
NguynThiThanhTho
 
PPTX
3_ppt_Layout.pptxgßbdbdbdbsbsbsbbsbsbsbsbsb
abhishekmathuroffici
 
PPT
14a-gui.ppt
DrDGayathriDevi
 
PPT
Windows Programming with AWT
backdoor
 
PPT
Chapter 5 GUI for introduction of java and gui .ppt
HabibMuhammed2
 
GUI Programming In Java
yht4ever
 
java swing
Waheed Warraich
 
Swing and AWT in java
Adil Mehmoood
 
Swingpre 150616004959-lva1-app6892
renuka gavli
 
swing_compo.pptxsfdsfffdfdfdfdgwrwrwwtry
zmulani8
 
Lecture 6.pdf
SakhilejasonMsibi
 
Graphical User Interface in JAVA
suraj pandey
 
Swing and Graphical User Interface in Java
babak danyal
 
tL19 awt
teach4uin
 
Graphical User Interface (GUI)
SAGARDAVE29
 
swingbasics
Arjun Shanka
 
JEDI Slides-Intro2-Chapter19-Abstract Windowing Toolkit and Swing.pdf
MarlouFelixIIICunana
 
03_GUI.ppt
DrDGayathriDevi
 
28-GUI application.pptx.pdf
NguynThiThanhTho
 
3_ppt_Layout.pptxgßbdbdbdbsbsbsbbsbsbsbsbsb
abhishekmathuroffici
 
14a-gui.ppt
DrDGayathriDevi
 
Windows Programming with AWT
backdoor
 
Chapter 5 GUI for introduction of java and gui .ppt
HabibMuhammed2
 

Recently uploaded (20)

PPTX
Eco-Friendly Living.ppt BIRLA OPEN MIND INTERNATIONAL SCHOOL
aartidevi7613
 
PPSX
Eagle in Paintings .ppsx
guimera
 
PPTX
Roadmap Infographics by Slidesgo.pptx qwerty
canigethellomoto
 
PDF
DOC-20250716-WA0020..pkjhnb.kjuvhjuhv.kihjdf
sayeedarhan829
 
PPTX
Lecture 1b - Diagnostic Analytics Intro and Purpose ver 1.0 (1).pptx
guddipatel10
 
PDF
Your Love Story, Beautifully Told by FliqaIndia’s Creative Experts
sandeep239227
 
PDF
Between Porchlight and Citylight: The Joseph Kim Journey
Joseph Kim Nolensville Tennessee
 
PPTX
UOA毕业证如何办理:奥克兰大学成绩单底纹防伪网上可查学历学位证书
xxxihn4u
 
PDF
FEDEX Crystal Plaques at Clazz Trophy Malaysia | #1 Reliable Trophy Supplier ...
Clazz Trophy Malaysia
 
PPTX
Una a presentación Designó do portal.pptx
defariasrosaschristi
 
PDF
File system ,data storage,FAT,NTFS,APFS and directory structures
jaydeepprajapati696
 
PDF
Ricardo Salinas Pliego Accused of Fraudulent Delisting of Grupo Elektra to En...
associate14
 
PPTX
UCSP Quarter 1 Week 4 Powerpoint Presentation
EmyMaquiling1
 
PPTX
Internet Download Manager (IDM) Crack Free Download [Latest 2025] PPTX
awaischaudary317
 
DOCX
This is a security test for Inspectiv test
Firsts Lasts
 
PDF
iTop VPN Crack New Version Latest 2025?
itskinga12
 
PDF
Star Crystal Trophies at Clazz Trophy Malaysia | #1 Reliable Trophy Supplier ...
Clazz Trophy Malaysia
 
PPT
preposition-powerpoint-aus-ver1_ver_4.ppt
NhungNgLHoi
 
PPTX
Elements of Music, Performing and Visual Arts
DyssaJaneManriqueAyn
 
PDF
📚Student and education importance......
kasimnagori121
 
Eco-Friendly Living.ppt BIRLA OPEN MIND INTERNATIONAL SCHOOL
aartidevi7613
 
Eagle in Paintings .ppsx
guimera
 
Roadmap Infographics by Slidesgo.pptx qwerty
canigethellomoto
 
DOC-20250716-WA0020..pkjhnb.kjuvhjuhv.kihjdf
sayeedarhan829
 
Lecture 1b - Diagnostic Analytics Intro and Purpose ver 1.0 (1).pptx
guddipatel10
 
Your Love Story, Beautifully Told by FliqaIndia’s Creative Experts
sandeep239227
 
Between Porchlight and Citylight: The Joseph Kim Journey
Joseph Kim Nolensville Tennessee
 
UOA毕业证如何办理:奥克兰大学成绩单底纹防伪网上可查学历学位证书
xxxihn4u
 
FEDEX Crystal Plaques at Clazz Trophy Malaysia | #1 Reliable Trophy Supplier ...
Clazz Trophy Malaysia
 
Una a presentación Designó do portal.pptx
defariasrosaschristi
 
File system ,data storage,FAT,NTFS,APFS and directory structures
jaydeepprajapati696
 
Ricardo Salinas Pliego Accused of Fraudulent Delisting of Grupo Elektra to En...
associate14
 
UCSP Quarter 1 Week 4 Powerpoint Presentation
EmyMaquiling1
 
Internet Download Manager (IDM) Crack Free Download [Latest 2025] PPTX
awaischaudary317
 
This is a security test for Inspectiv test
Firsts Lasts
 
iTop VPN Crack New Version Latest 2025?
itskinga12
 
Star Crystal Trophies at Clazz Trophy Malaysia | #1 Reliable Trophy Supplier ...
Clazz Trophy Malaysia
 
preposition-powerpoint-aus-ver1_ver_4.ppt
NhungNgLHoi
 
Elements of Music, Performing and Visual Arts
DyssaJaneManriqueAyn
 
📚Student and education importance......
kasimnagori121
 
Ad

Swing demo presentation for the swing java

  • 1. CIS 068 Welcome to CIS 083 ! Introduction to GUIs: JAVA Swing
  • 2. CIS 068 Overview • JAVA and GUIs: SWING – Container, Components, Layouts – Using SWING
  • 3. CIS 068 The First Swing Program Example: The First Swing Program
  • 4. CIS 068 The GUI Container: JFrame Layout: BorderLayout North Center Components: JLabel JButton, containing an ImageIcon
  • 5. CIS 068 Steps to build a GUI 1. import package 2. set up top level container (e.g. JFrame) 3. apply layout (e.g. BorderLayout) 4. add components (e.g. Label, Button) 5. REGISTER listeners 6. show it to the world !
  • 6. CIS 068 The Source . import package 2. set up top level container (e.g. JFrame) 3. apply layout (e.g. BorderLayout) 4. add components (e.g. Label, Button) 5. REGISTER listeners 6. show it to the world !
  • 7. CIS 068 Swing Components • Top Level Containers • General Purpose Containers • Special Purpose Containers • Basic Controls • Uneditable Information Displays • Interactive Displays of Highly Formatted Information
  • 8. CIS 068 Swing Components Top Level Containers Your application usually extends one of these classes !
  • 9. CIS 068 Swing Components General Purpose Containers
  • 10. CIS 068 Swing Components General Purpose Containers • typically used to collect Basic Controls (JButton, JChoiceBox…) • Added to layout of top-level containers JPanel JFrame
  • 11. CIS 068 Swing Components Special Purpose Containers
  • 12. CIS 068 Swing Components Special Purpose Containers • If you want to use them, go to java.sun.com
  • 14. CIS 068 Swing Components Basic Controls • Unlike ‘passive’ containers, controls are the ‘active’ part of your GUI Remark: containers aren’t only ‘passive’, they are also ‘active’ sources of events, eg. Mouse-events. • Being the visible part of your interface, controls bring your application to life • Controls are event sources ! • Objects of your application register to controls to handle the events
  • 15. CIS 068 Swing Components Uneditable Information Displays
  • 16. CIS 068 Swing Components Interactive Displays of Highly Formatted Information
  • 17. CIS 068 Swing Components Interactive Displays of Highly Formatted Information • Define standard interfaces for frequently needed tasks ... go to java.sun.com for further information ...
  • 18. CIS 068 Layout Management How to glue it all together: The Layout Management
  • 19. CIS 068 Layout Management • The process of determining the size and position of components • A layout manager is an object that performs layout management for the components within the container. • Layout managers have the final say on the size and position of components added to a container • Using the add method to put a component in a container, you must ALWAYS take the container's layout manager into account
  • 20. CIS 068 Layout Management ... and finally, the layout manager preserves the world from home made layout-design !
  • 21. CIS 068 Layout Management Java supplies five commonly used layout managers: 1. BorderLayout 2. BoxLayout 3. FlowLayout 4. GridBagLayout 5. GridLayout
  • 22. CIS 068 Layouts BorderLayout Position must be specified, e.g. add (“North”, myComponent)
  • 23. CIS 068 Layouts BoxLayout The BoxLayout class puts components in a single row or column. It respects the components‘ requested maximum sizes.
  • 24. CIS 068 Layouts FlowLayout FlowLayout is the default layout manager for every JPanel. It simply lays out components from left to right, starting new rows if necessary
  • 25. CIS 068 Layouts GridBagLayout GridBagLayout is the most sophisticated, flexible layout manager the Java platform provides. If you really want to use it, go to java.sun.com …
  • 26. CIS 068 Layouts GridLayout GridLayout simply makes a bunch of components equal in size and displays them in the requested number of rows and columns .
  • 27. CIS 068 Using Components Examples: • Using a JButton • Using a JSlider • Using a JCheckBox
  • 28. CIS 068 Using a JButton Some Constructors: JButton() Creates a button with no text or icon JButton(Icon icon) Creates a button with an icon JButton(String text) Creates a button with text JButton(String text, Icon icon) Creates a button with initial text and an icon
  • 29. CIS 068 Using a JButton Some Methods: addActionListener( ActionListener a) Registers ActionListener to JButton Inherited from AbstractButton setFont(Font font) Specifies Font (Type, Style, Size) Inherited from JComponent setBackground( Color color) Sets background color Inherited from JComponent setActionCommand (String text) Used to specify button if listener is registered to multiple buttons (see ActionEvent.getActionCommand())
  • 30. CIS 068 Using a JSlider Some Constructors: JSlider() Creates a horizontal slider with the range 0 to 100 and an initial value of 50 JSlider( int min, int max, int value) Creates a horizontal slider using the specified min, max and value. JSlider( Int orientation int min, int max, int value) Creates a slider with the specified orientation and the specified minimum, maximum, and initial values.
  • 31. CIS 068 Using a JSlider Some Methods: addChangeListener (ChangeListener cl) Registers ChangeListener to slider int getValue() Returns the slider’s value setValue(int value) Sets the slider’s value
  • 32. CIS 068 Using a JCheckBox Some Constructors: JCheckBox() Creates an initially unselected check box button with no text, no icon. JCheckBox( String text) Creates an initially unselected check box with text. JCheckBox( String text, Icon icon, boolean selecte d) Creates a check box with text and icon, and specifies whether or not it is initially selected.
  • 33. CIS 068 Using a JCheckBox Some Methods: addItemListener (ItemListener il) Registers ItemListener to checkbox Inherited from AbstractButton setSelected( boolean select) Sets the state of checkbox Inherited from AbstractButton boolean getSeleted() Gets the state of checkbox. calling method often saves from registering to the checkbox !
  • 34. CIS 068 Custom Painting creating your own graphics: Custom Painting
  • 35. CIS 068 Custom Painting Decide which superclass to use, for example: • JPanel: Generating and displaying graphs in top of a blank or transparent background • JLabel: Painting on top of an image • JButton: custom button • … Every class derived from JComponent can be used for custom drawing ! (Recommended: JPanel)
  • 36. CIS 068 Custom Painting The Graphics Object • provides both a context for painting and methods for performing the painting. • Example of methods – drawImage – drawString – drawRect – fillRect – setColor – … • passed as argument to the paintComponent - method
  • 37. CIS 068 Custom Painting The paintComponent method • Method of class JComponent • Inherited to all subclasses, e.g. JPanel, JButton,… • The place where all custom painting belongs ! • Invoked by the event-scheduler or by the repaint() - method
  • 39. CIS 068 At last... This was a BRIEF overview and introduction to SWING. SWING has MUCH more to offer, see • http://java.sun.com/docs/books/tutorial/uiswing/ • http://java.sun.com/j2se/1.4.1/docs/api/