Swing
Swing
UNIT - III
M.S. CHAHAR
Astt. Professor
R.B.S.M.T.C., Agra
9997775422
1 M.S.Chahar
Introduction to JFC: (JAVA FOUNDATION 2
CLASS)
Swing is the part of the Java Foundation class. java AWT provides a
little but reasonable set of user interface components.
The JFC implements a new set of GUI components with a pluggable
look and feel. Swing is implemented in pure java and is based on
jdk1.1 light weight user interface framework.
JFC 1.2 has more then 250 classes. The five major Package.
Swing.
Pluggable look and Feel.
Drag and Drop.
Accessibility.
2D Graphics.
M.S.Chahar
3
The pluggable look and feel lets you design a single set of
GUI components that components are written entirely in
java.
Drag and Drop provides data transfer across both java and
used by people with disabilities. Such as Braille terminals,
speech recognition system and screen readers.
Java 2D API provides advanced capabilities for
working with shapes, text and images.
M.S.Chahar
4
Swing has many advanced features like JTabel, Jtabbed pane which is not
available in AWT. Also. Swing components are called "lightweight" because they
JButton, JTextArea, etc., are lightweight because they do not have an OS peer.
With AWT, you have 21 "peers" (one for each control and one for the dialog itself). A
With Swing, you would have only one peer, the operating system's window object.
All of the buttons, entry fields, etc. are drawn by the Swing package on the drawing
surface provided by the window object. This is the reason that Swing has more
code. It has to draw the button or other control and implement its behavior instead
M.S.Chahar
AWT 7
M.S.Chahar
8
AWT COMPONENT
M.S.Chahar
9
The Java Foundation Classes consist of five major parts:
AWT, Swing, Accessibility, Java 2D, and Drag and Drop.
Java 2D has become an integral part of AWT.
Swing is built on top of AWT, and Accessibility support is
built into Swing.
The five parts of JFC are certainly not mutually
exclusive, and Swing is expected to merge more
deeply with AWT in future versions of Java.
The Drag and Drop API was far from mature at the time
of this writing but we expect this technology to
integrate further with Swing and AWT in the near future.
Thus, AWT is at the core of JFC, which in turn makes it
M.S.Chahar
one of the most important libraries in Java 2.
10
Swing
Swing is a large set of components ranging from
the very simple, such as labels, tables, trees, and
styled text documents.
All Swing components are derived from a single
parent called JComponent which extends the
AWT Container class. Thus, Swing is best described
as a layer on top of AWT rather than a
replacement for it.
AWT component there is a Swing equivalent with
prefix “J”. The only exception to this is the AWT
Canvas class, for which JComponent, JLabel, or
Jpanel. You will also notice many Swing classes
with no AWT counterparts. M.S.Chahar
11
Swing Component
M.S.Chahar
Swing Package
12
javax.swing
Contains the most basic Swing components.
javax.swing.border
Classes and interfaces used to define specific
border styles. Note: That borders can be shared by any
number of Swing components, as they are not
components themselves.
javax.swing.colorchooser
Classes and interfaces supporting the
JColorChooser component, used for color selection.
javax.swing.event
The event package contains all Swing-specific
event types and listeners. Swing components also
support events and listeners defined in java.awt.event
and java.beans. M.S.Chahar
13
javax.swing.filechooser
Classes and interfaces supporting the JFileChooser
component, used for file selection.
javax.swing.plaf
Contains the pluggable look-and-feel API used to define
custom user interface components. Most of the classes in this
package are abstract. They are subclassed and implemented
by look-and-feel implementations such as metal, motif, and
basic. The classes in this package are intended for use only by
developers who, for one reason or another, cannot build on top
of existing look-and-feels.
javax.swing.plaf.basic
Consists of the Basic look-and-feel implementation which
all look-and-feels provided with Swing are built on top of. We are
normally expected to subclass the classes in this package if we
want to create our own customized look-and-feel.
javax.swing.plaf.metal
Metal is the default look-and-feel of Swing components.
It is the only look-and-feel that ships with Swing not designed to
be consistent with a specific platform.
M.S.Chahar
14
javax.swing.plaf.multi
This is the Multiplexing look-and-feel. This is not a regular
look-and-feel implementation in that it does not define the
actual look or feel of any components. Rather, it provides the
ability to combine several look-and-feels for simultanteous use.
A typical example might be using an audio-based look-and-
feel in combination with metal or motif. Currently Java 2 does
not ship with any multiplexing look-and-feel implemenations
(however, rumor has it that the Swing team is working on an
audio look-and-feel as we write this).
javax.swing.table
Classes and interfaces supporting the JTable control. This
component is used to manage tabular data in spreadsheet
form. It supports a high degree of customization without
requiring look-and-feel enhancements.
javax.swing.text
Classes and interfaces used by the text components
including support for plain and styled documents, the views of
those documents, highlighting, caret control and customization,
editor actions and keyboard customization. M.S.Chahar
15
Navigation Through Look & Feel:
Swing facilitates plaf; which means pluggable look and
feel. The visual appearance of a GUI along with its visual
objects is called LOOK component. An event if fired of
generated on any of those visual objects, the
mechanism through which the event is handled is
termed as FEEL component. Swing facilitates three types
of LOOK and FEEL.
Meta LOOK & FEEL.
Motif LOOK and FEEL.
Window LOOK and FEEl or Basic.
Java is either a class or an object, therefore the above
mentioned LOOK and FEEL are classes of Java.swing.plaf
package. The default look and feel chosen, whenever
we design a swing application is Metal. The
setLookAndFeel() is used to change the default LOOK
and FEEL component of swing application.
The Motif Look and Feel one has to send the string
“com.sun.java.swing.plaf.motif.MotifLookAndFeel” as an
M.S.Chahar
argument to setLookAndFeel().
16
MVC architecture
Model-view-controller architecture
M.S.Chahar
18
Model
M.S.Chahar
19
View
M.S.Chahar
20
Controller
M.S.Chahar
21
M.S.Chahar
22
JComponent Class
Swing components inherit directly or indirectly from the
JComponent class, an indirect subclass of the old AWT
Component class. Since there are a few Swing
components classes that do not inherit from JComponent,
it is useful to divide the JComponent methods into two
types: those that are inherited indirectly from the old AWT
Component class, and those that are defined in the
JComponent class itself.
Old AWT Methods
void setBackground(Color c)
void setForeground(Color c)
void setFont(Font f)
void setEnabled(boolean b)
void setVisible(boolean b)
M.S.Chahar
23
JComponent Methods
These methods can only be used with Swing components
that inherit directly or indirectly from the JComponent
class. The first three are used to get components sized
correctly, but you need to know what the layout manager
does with the sizes.
void setMinimumSize(Dimension dim)
void setPreferredSize(Dimension dim)
void setMaximumSize(Dimension dim)
void setBorder(Border b)
void setToolTipText(String txt)
M.S.Chahar
Container Class
24
The Container class is an old AWT class that extends the old AWT
Component class. A Container object does not have any visual appetence
of its own except as a background color for a region on the screen. Its
importance stems from the fact that it can contain other components. It
uses a layout manager to determine where its components are placed
and how they are sized.
There are two kinds of containers in Swing: JPanel objects and content
panes. Like most objects, JPanel objects are created using their class
construcors. Content panes, on the other hand, are obtained by sending a
getContentPane() message to a top-level window (a JApplet, JFrame,
JWindow, JDialog, or JInternalFrame object).
Both JPanel objects and content panes indirectly inherit all of the old AWT
Component methods. Since the JPanel class extends the JComponent
class, its objects also inherit the JComponent methods.
M.S.Chahar
25
The component methods, all containers inherit the
container methods listed below.
void setLayout(LayoutManager lm)
void add(JComponent c)
void add(JComponent c, Object whr)
The layout manager parameter in setLayout() is
usually constructed on the spot with a new
expression such as new FlowLayout(). The second
version of add() is usually only needed with a
BorderLayout. Its whr parameter then specifies which
region the component should be placed in. See
BorderLayout.
M.S.Chahar
26
JApplet Class
The JApplet class is never used directly.
Instead, you write a class that extends
JApplet, providing an implementation of its
init() method. When the applet appears in a
browser window, the browser invokes the
init() method. You write statements in the init
method to construct and arrange the
components and set up listeners to handle
user keyboard and mouse actions.
void init()
Container getContentPane()
void setJMenuBar(JMenuBar menuBar)
M.S.Chahar
27
JPanel Class
JLabel Class
JButton Class
JComboBox Class
void addItem(Object itm)
void setSelectedItem(Object itm)
void addItemListener(ItemListener
l)
Object getSelectedItem()
JTextField Class
M.S.Chahar
JSlider Class 28
M.S.Chahar
30
M.S.Chahar