Transcript Swing
Swing
9-Apr-16
R Ramesh
An Introduction to Java Foundation
Classes (JFC)
A suite of libraries to assist
programmers create enterprise
applications.
Extends the original Abstract Window
Toolkit (AWT) by adding a
comprehensive set of graphical user
interface class libraries
9-Apr-16
R Ramesh
The Five API’s of JFC
Abstract window toolkit (AWT)
Swing
Accessibility
Java 2D
Drag and Drop
9-Apr-16
R Ramesh
AWT
Basic GUI toolkit
New event model
Downward compatibility for java
versions less than 1.1
9-Apr-16
R Ramesh
Swing
Built on top of the core 1.1 and 1.2
AWT libraries
Has twice the number of components
as AWT
Cleaner design throughout
Depends on event handling mechanism
of AWT 1.1
9-Apr-16
R Ramesh
Accessibility
Assistance to users who have trouble
with traditional user interfaces
Used in conjunction with devices such
as audible text readers or braille
keyboards
9-Apr-16
R Ramesh
Java 2D
Classes for implementing various
painting styles, complex shapes, fonts
and colors
Based on APIs that were licensed from
IBM’s Taligent division
Not a part of Swing
9-Apr-16
R Ramesh
Drag and Drop
Moving objects in and out of window or
frame with predictable results
Can be used to transfer information
between Java applications and native
applications
Not a part of Swing
9-Apr-16
R Ramesh
The Five API’s of JFC
9-Apr-16
R Ramesh
Why Swing and Why Not
AWT?
AWT provided minimum functionality
necessary to create a windowing
application
Swing is not a replacement for AWT but
built on top of core AWT libraries
9-Apr-16
R Ramesh
Disadvantages of AWT
Uses platform’s native components
Minimal number of components
Timing and sequence problems
between platforms
9-Apr-16
R Ramesh
Why Swing?
Pluggable look-and-feels
Lightweight components
Other features
Wide variety of components
Support for rendering custom Swing
components
Support for replacing their insets with
arbitrary concentric borders
9-Apr-16
R Ramesh
Model View Controller (MVC)
Architecture
Design Pattern introduced as a part of
Smalltalk-80 version of Smalltalk
programming language
Build systems that made use of
multiple, synchronized presentation of
same data
9-Apr-16
R Ramesh
MVC may be used in
A graph package that contains
different graphical view of same data
A CAD system, in which portions of
design can be viewed at different
magnifications, in different windows
and at different scales
9-Apr-16
R Ramesh
9-Apr-16
R Ramesh
9-Apr-16
R Ramesh
Model
Represents state and low-level behavior
of the component
Manages state and conducts
transformations on that state
Has no specific knowledge of either its
controllers or its views
9-Apr-16
R Ramesh
View
Manages visual display of the state
represented by model
A model can have more than one view
Swing set has one view associated with
one model
9-Apr-16
R Ramesh
Controller
Manages user interaction with the
model
Has mechanism by which changes are
made to the state of the model
9-Apr-16
R Ramesh
Benefits of MVC
Clearly defined separation between
components of a program
Problems in each domain can be solved
independently
Binding between the model and the
view is dynamic
9-Apr-16
R Ramesh
MVC in Swing Components
9-Apr-16
R Ramesh
The Model
The methods under model can be
grouped as
Query internal state
Manipulate internal state
Add and remove event listeners
Fire events
9-Apr-16
R Ramesh
The View and Controller
Responsible for creating visual
representation and handling user input
Methods are
Paint
Return geometric information
Handle AWT events
9-Apr-16
R Ramesh
JComponents and Subclasses
Abstract class extended by all Swing
components
Contains properties such as,
Background/foreground
Enable/Disable
Visibility
9-Apr-16
R Ramesh
JComponent’s Ancestry
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
9-Apr-16
R Ramesh
Top Level Components
JApplet
JDialog
JFrame
JWindow
derived from their
AWT version
9-Apr-16
R Ramesh
Components Under
JComponent
JComboBox
JScrollPane
JLabel
JTable
JList
JTree
JProgressBar JRootPane
JMenuBar
JPanel
JInternalFrame
JSeparator
JFileChooser
JLayeredPane
JDesktopPane
JOptionPane
JSlider
JTabbedPane JToolBar JToolTip JViewport
JPopupMenu
JSplitPane
JColorChooser
JTextComponent
JEditorPane
JTextField
JTextPane
JPasswordField
JTextArea
9-Apr-16
R Ramesh
Components Under
JComponent Contd..
Abstract Button
JCheckBox
JToggleButton
JButton
JRadioButton
JMenuItem
JMenu
JRadioButtonMenuItem
JCheckButtonMenuItem
9-Apr-16
R Ramesh
What JComponent Provides
Invalidating and repainting
Position, size and alignment
Adding Borders
Tooltips
Double Buffering
Focus and Focus Cycle
9-Apr-16
R Ramesh
JFrame
Standard window
Retains the look and feel of host OS
Acts as the main window to an
application
9-Apr-16
R Ramesh
JWindow
Floats anywhere on the screen
Has no frame around it
Typically used for popup menus
To display Splash screens
9-Apr-16
R Ramesh
JDialog
Floats anywhere on the screen
Can be modal or non-modal
Used to display messages
The border and title bar are defined by
the OS
9-Apr-16
R Ramesh
JMenu
Attached to a menu bar (JMenuBar) or
another JMenu
Items are added using JMenuItem
Items can be a JMenuItem and/or
JCheckBoxMenuItem and/or
JRadioButtonMenuItem
9-Apr-16
R Ramesh
JPanel
A container to add components or to
draw.
A non-abstract version of JComponent
Common use is to group components
9-Apr-16
R Ramesh
JScrollPane
Helps to scroll around panels
Automatically adjusts itself
9-Apr-16
R Ramesh
JSplitPane
A container which can hold two
components
Can be split horizontally or vertically
9-Apr-16
R Ramesh
JTabbedPane
To display more components in one
screen
Components are grouped into
categories
Each tab holds a category of
components
9-Apr-16
R Ramesh
ImageIcon
Load small Images
Used when needed
Used on Buttons, Check boxes, Radio
buttons etc.
9-Apr-16
R Ramesh
JToolBar
A series of icon buttons
Used as shortcuts to tasks
A tool tip is associated with each button
A tool bar can be in a separate window
9-Apr-16
R Ramesh
JSlider and JProgressBar
A JSlider has minimum, maximum and a
current value
Units on a JSlider can be displayed
using tick marks and labels
A JProgressBar is used to display the
progress of a task
9-Apr-16
R Ramesh
JTable
Helps to create spreadsheet like
controls that have rows and columns
Editable cells
Cell renderers can be used to add
components like checkbox into a cell
9-Apr-16
R Ramesh
JTree
To display data in a hierarchical way
One root node from which all nodes
descend
Nodes can be collapsed/expanded
9-Apr-16
R Ramesh
Summary
JFC
Between Swing and AWT
MVC Architecture
JComponent and its sub classes
9-Apr-16
R Ramesh