SlideShare a Scribd company logo
Swing Introduction
2
Introduction to Java Swing
3
AWT vs. Swing
Abstract Windowing Toolkit (AWT)
• Original Java GUI toolkit
• Wrapper API for native GUI components
• Lowest-common denominator for all Java host
environments
Swing
• Implemented entirely in Java on top of AWT
• Richer set of GUI components
• Pluggable look-and-feel support
4
Swing Design Principles
• GUI is built as containment hierarchy of widgets
(i.e. the parent-child nesting relation between
them)
• Event objects and event listeners
– Event object: is created when event occurs (e.g. click),
contains additional info (e.g. mouse coordinates)
– Event listener: object implementing an interface with
an event handler method that gets an event object as
argument
• Separation of Model and View:
– Model: the data that is presented by a widget
– View: the actual presentation on the screen
5
Partial AWT and Swing
Class Hierarchy
java.lang.Object
Component MenuComponent
CheckboxGroup
Button Checkbox
Canvas Choice Container Label List Scrollbar TextComponent
JComponent Window
Frame
JFrame
Dialog
JDialog
Panel
Scrollpane
Applet
JApplet
java.awt.* javax.swing.*
JLabel JList
AbstractButton
JButton
JPanel JScrollpane
6
Swing Widgets (more on this next lecture)
Top-Level Containers
JFrame JPanel
JSplitPane
JTabbedPane
JDialog
JScrollPane
General-Purpose Containers
JButton
JCheckbox JRadioButton
and ButtonGroup
JCombobox
JLabel
JList Menu
7
More Swing Widgets
JColorChooser
JFileChooser
JTree
JTable
8
The Initial Swing GUI
Containment Hierarchy
File Edit
Undo
Redo
Cut
Frame / Dialog / Applet
Root Pane
Layered Pane
Content Pane
Glass Pane
a 3D model
enables menus to
pop up above the
content pane
allows for
interception of
mouse events and
painting across
GUI components
9
The Initial Swing GUI
Containment Hierarchy
aTopLevelContainer:
JFrame or JDialog or JApplet
rootPane:
JRootPane
(JPanel) glassPane:
java.awt.Component
(JPanel) contentPane:
java.awt.Container
layeredPane:
JLayeredPane
menuBar:
JMenuBar
optional
10
Swing Hello World
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class HelloWorld {
public static void main(String[] args) {
JFrame frame = new JFrame("Hello World!");
frame.setSize(220, 200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
contentPane.setLayout(null);
JButton button = new JButton("Hello World!");
button.setLocation(30, 30);
button.setSize(150, 100);
contentPane.add(button);
frame.setVisible(true);
}
}
11
Swing Hello World
with Events
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MyActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Toolkit.getDefaultToolkit().beep();
}
}
...
public class HelloWorld {
public static void main(String[] args) {
...
JButton button = new JButton("Hello World!");
button.addActionListener(new MyActionListener());
...
}
}
12
Containment Hierarchy
of a Menu
…
public class MenuExample {
public static void main(String[] args) {
JFrame frame = new JFrame("My Frame");
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
JMenu fileMenu = new JMenu("File");
fileMenu.add(new JMenuItem("New"));
fileMenu.add(new JMenuItem("Open"));
fileMenu.add(new JMenuItem("Close"));
JMenu editMenu = new JMenu("Edit");
editMenu.add(new JMenuItem("Undo"));
editMenu.add(new JMenuItem("Redo"));
editMenu.add(new JMenuItem("Cut"));
JMenuBar menubar = new JMenuBar();
menubar.add(fileMenu);
menubar.add(editMenu);
frame.setJMenuBar(menubar);
frame.setVisible(true);
} }
File Edit
Undo
Redo
Cut
File Edit
New
Open
Close
13
Handling Menu Events
...
public class MenuActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(MenuExample.frame,
"Got an ActionEvent at " + new Date(e.getWhen())
+ " from " + e.getSource().getClass());
} }
...
public class MenuExample {
static JFrame frame;
public static void main(String[] args) {
...
JMenuItem item = new JMenuItem("Close");
item.addActionListener(new MenuActionListener());
fileMenu.add(item);
...
} }
14
Defining Event Listeners with Anonynous
Classes
• Use new Classname() {…} or new Interfacename(){…} to
create a single object of an anonymous subclass of the given
class/interface
• Anonymous classes can access final variables of their context (i.e.
final variables of the method or class they are created in)
...
public class MenuExample {
public static void main(String[] args) {
final JFrame frame = new JFrame("My Frame");
...
JMenuItem item = new JMenuItem("Close");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int n = JOptionPane.showOptionDialog(frame,...
}
});
...
} }
15
Different Kinds of
Swing Events
Low-level events
• MouseEvent: Component got mouse-down, mouse-move, etc.
• KeyEvent: Component got key-press, key-release, etc.
• ComponentEvent: Component resized, moved, etc.
• ContainerEvent: Container's contents changed because a component
was added or removed
• FocusEvent: Component got focus or lost focus
• WindowEvent: Window opened, closed, etc.
High-level semantic events
• ActionEvent: Main action of control invoked (e.g. JButton click)
• AdjustmentEvent: Value was adjusted (e.g. JScrollBar moved)
• ItemEvent: Item was selected or deselected (e.g. in JList)
• TextEvent: Text in component has changed (e.g in JTextField)
16
Events, Listeners, Adapters and
Handler Methods
Event Listener / Adapter Handler Methods
ActionEvent ActionListener actionPerformed
AdjustmentEvent AdjustmentListener adjustmentValueChanged
MouseEvent MouseListener
MouseAdapter
mouseClicked
mouseEntered
mouseExited
mousePressed
mouseReleased
KeyEvent KeyListener
KeyAdapter
keyPressed
keyReleased
keyTyped
ComponentEvent ComponentListener
ComponentAdapter
componentShown
componentHidden
componentMoved
componentResized
Adapter classes with empty methods for Listener interfaces with >1 methods
17
Summary
• Desktop environments consist of:
– Windowing System: handles input/output
– Widget Toolkit:
draws widgets and dispatches their events
– Window Manager: takes care of windows
• Swing is a widget toolkit for Java
– GUI as containment hierarchy of widgets
– Event objects and event listeners
References:
http://java.sun.com/docs/books/tutorial/uiswing/
http://www.javabeginner.com/java-swing-tutorial.htm

More Related Content

PDF
11basic Swing
PDF
Basic swing
PPT
Swing_Introduction and working java.ppt
PPTX
JAVA AWT
PPT
01 Java Is Architecture Neutral
PPT
PPTX
PDF
Short Intro to Android Fragments
11basic Swing
Basic swing
Swing_Introduction and working java.ppt
JAVA AWT
01 Java Is Architecture Neutral
Short Intro to Android Fragments

Similar to Swing_Introduction.ppt (20)

PPTX
GUI Programming with Java
PPTX
event-handling.pptx
PPT
09events
PDF
Z blue introduction to gui (39023299)
PPTX
swings.pptx
PPT
Chap1 1.1
PPT
Chap1 1 1
PPT
Graphical User Interface (GUI) - 2
PPT
engineeringdsgtnotesofunitfivesnists.ppt
PPT
Unit 5.133333333333333333333333333333333.ppt
PPTX
Unit 4_1.pptx JDBC AND GUI FOR CLIENT SERVER
PDF
JavaFXaeurwstkiryikryiuyoyiloyuikygi.pdf
DOCX
Lecture9 oopj
PPT
Java eventhandling
PDF
package net.codejava.swing.mail;import java.awt.Font;import java.pdf
PDF
Swing
PDF
Swing
PPTX
it's about the swing programs in java language
KEY
SWTBot Tutorial
GUI Programming with Java
event-handling.pptx
09events
Z blue introduction to gui (39023299)
swings.pptx
Chap1 1.1
Chap1 1 1
Graphical User Interface (GUI) - 2
engineeringdsgtnotesofunitfivesnists.ppt
Unit 5.133333333333333333333333333333333.ppt
Unit 4_1.pptx JDBC AND GUI FOR CLIENT SERVER
JavaFXaeurwstkiryikryiuyoyiloyuikygi.pdf
Lecture9 oopj
Java eventhandling
package net.codejava.swing.mail;import java.awt.Font;import java.pdf
Swing
Swing
it's about the swing programs in java language
SWTBot Tutorial
Ad

More from Satyanandaram Nandigam (10)

PDF
Introduction_SE_Modifiedsoftware engineering.pdf
PPTX
Data Modelling Conceptual level physical.pptx
PPTX
Basics_of_Object Oriented Programming_in_CPP.pptx
PPTX
Java_Access_Specifiers_and_Data_Types.pptx
PPTX
Java Nested classes, static class and methods, nested blocks_Inner_Classes.pptx
PPT
software Design.ppt
PPT
C++_overloading.ppt
PPTX
Review of C.pptx
PPTX
Introduction_SE_Modifiedsoftware engineering.pdf
Data Modelling Conceptual level physical.pptx
Basics_of_Object Oriented Programming_in_CPP.pptx
Java_Access_Specifiers_and_Data_Types.pptx
Java Nested classes, static class and methods, nested blocks_Inner_Classes.pptx
software Design.ppt
C++_overloading.ppt
Review of C.pptx
Ad

Recently uploaded (20)

PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Current and future trends in Computer Vision.pptx
PPTX
web development for engineering and engineering
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
UNIT 4 Total Quality Management .pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
Geodesy 1.pptx...............................................
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
additive manufacturing of ss316l using mig welding
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Model Code of Practice - Construction Work - 21102022 .pdf
Current and future trends in Computer Vision.pptx
web development for engineering and engineering
R24 SURVEYING LAB MANUAL for civil enggi
UNIT 4 Total Quality Management .pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
OOP with Java - Java Introduction (Basics)
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
CH1 Production IntroductoryConcepts.pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Geodesy 1.pptx...............................................
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
bas. eng. economics group 4 presentation 1.pptx
additive manufacturing of ss316l using mig welding
Operating System & Kernel Study Guide-1 - converted.pdf
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx

Swing_Introduction.ppt

  • 3. 3 AWT vs. Swing Abstract Windowing Toolkit (AWT) • Original Java GUI toolkit • Wrapper API for native GUI components • Lowest-common denominator for all Java host environments Swing • Implemented entirely in Java on top of AWT • Richer set of GUI components • Pluggable look-and-feel support
  • 4. 4 Swing Design Principles • GUI is built as containment hierarchy of widgets (i.e. the parent-child nesting relation between them) • Event objects and event listeners – Event object: is created when event occurs (e.g. click), contains additional info (e.g. mouse coordinates) – Event listener: object implementing an interface with an event handler method that gets an event object as argument • Separation of Model and View: – Model: the data that is presented by a widget – View: the actual presentation on the screen
  • 5. 5 Partial AWT and Swing Class Hierarchy java.lang.Object Component MenuComponent CheckboxGroup Button Checkbox Canvas Choice Container Label List Scrollbar TextComponent JComponent Window Frame JFrame Dialog JDialog Panel Scrollpane Applet JApplet java.awt.* javax.swing.* JLabel JList AbstractButton JButton JPanel JScrollpane
  • 6. 6 Swing Widgets (more on this next lecture) Top-Level Containers JFrame JPanel JSplitPane JTabbedPane JDialog JScrollPane General-Purpose Containers JButton JCheckbox JRadioButton and ButtonGroup JCombobox JLabel JList Menu
  • 8. 8 The Initial Swing GUI Containment Hierarchy File Edit Undo Redo Cut Frame / Dialog / Applet Root Pane Layered Pane Content Pane Glass Pane a 3D model enables menus to pop up above the content pane allows for interception of mouse events and painting across GUI components
  • 9. 9 The Initial Swing GUI Containment Hierarchy aTopLevelContainer: JFrame or JDialog or JApplet rootPane: JRootPane (JPanel) glassPane: java.awt.Component (JPanel) contentPane: java.awt.Container layeredPane: JLayeredPane menuBar: JMenuBar optional
  • 10. 10 Swing Hello World import java.awt.*; import java.awt.event.*; import javax.swing.*; public class HelloWorld { public static void main(String[] args) { JFrame frame = new JFrame("Hello World!"); frame.setSize(220, 200); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container contentPane = frame.getContentPane(); contentPane.setLayout(null); JButton button = new JButton("Hello World!"); button.setLocation(30, 30); button.setSize(150, 100); contentPane.add(button); frame.setVisible(true); } }
  • 11. 11 Swing Hello World with Events import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MyActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { Toolkit.getDefaultToolkit().beep(); } } ... public class HelloWorld { public static void main(String[] args) { ... JButton button = new JButton("Hello World!"); button.addActionListener(new MyActionListener()); ... } }
  • 12. 12 Containment Hierarchy of a Menu … public class MenuExample { public static void main(String[] args) { JFrame frame = new JFrame("My Frame"); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); JMenu fileMenu = new JMenu("File"); fileMenu.add(new JMenuItem("New")); fileMenu.add(new JMenuItem("Open")); fileMenu.add(new JMenuItem("Close")); JMenu editMenu = new JMenu("Edit"); editMenu.add(new JMenuItem("Undo")); editMenu.add(new JMenuItem("Redo")); editMenu.add(new JMenuItem("Cut")); JMenuBar menubar = new JMenuBar(); menubar.add(fileMenu); menubar.add(editMenu); frame.setJMenuBar(menubar); frame.setVisible(true); } } File Edit Undo Redo Cut File Edit New Open Close
  • 13. 13 Handling Menu Events ... public class MenuActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(MenuExample.frame, "Got an ActionEvent at " + new Date(e.getWhen()) + " from " + e.getSource().getClass()); } } ... public class MenuExample { static JFrame frame; public static void main(String[] args) { ... JMenuItem item = new JMenuItem("Close"); item.addActionListener(new MenuActionListener()); fileMenu.add(item); ... } }
  • 14. 14 Defining Event Listeners with Anonynous Classes • Use new Classname() {…} or new Interfacename(){…} to create a single object of an anonymous subclass of the given class/interface • Anonymous classes can access final variables of their context (i.e. final variables of the method or class they are created in) ... public class MenuExample { public static void main(String[] args) { final JFrame frame = new JFrame("My Frame"); ... JMenuItem item = new JMenuItem("Close"); item.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int n = JOptionPane.showOptionDialog(frame,... } }); ... } }
  • 15. 15 Different Kinds of Swing Events Low-level events • MouseEvent: Component got mouse-down, mouse-move, etc. • KeyEvent: Component got key-press, key-release, etc. • ComponentEvent: Component resized, moved, etc. • ContainerEvent: Container's contents changed because a component was added or removed • FocusEvent: Component got focus or lost focus • WindowEvent: Window opened, closed, etc. High-level semantic events • ActionEvent: Main action of control invoked (e.g. JButton click) • AdjustmentEvent: Value was adjusted (e.g. JScrollBar moved) • ItemEvent: Item was selected or deselected (e.g. in JList) • TextEvent: Text in component has changed (e.g in JTextField)
  • 16. 16 Events, Listeners, Adapters and Handler Methods Event Listener / Adapter Handler Methods ActionEvent ActionListener actionPerformed AdjustmentEvent AdjustmentListener adjustmentValueChanged MouseEvent MouseListener MouseAdapter mouseClicked mouseEntered mouseExited mousePressed mouseReleased KeyEvent KeyListener KeyAdapter keyPressed keyReleased keyTyped ComponentEvent ComponentListener ComponentAdapter componentShown componentHidden componentMoved componentResized Adapter classes with empty methods for Listener interfaces with >1 methods
  • 17. 17 Summary • Desktop environments consist of: – Windowing System: handles input/output – Widget Toolkit: draws widgets and dispatches their events – Window Manager: takes care of windows • Swing is a widget toolkit for Java – GUI as containment hierarchy of widgets – Event objects and event listeners References: http://java.sun.com/docs/books/tutorial/uiswing/ http://www.javabeginner.com/java-swing-tutorial.htm