0% found this document useful (0 votes)
27 views30 pages

Swing

Swing is the graphical user interface (GUI) toolkit that is part of Java Foundation Classes. It provides a set of lightweight components that are more advanced than the Abstract Window Toolkit (AWT) components. Some key features of Swing include its use of lightweight components, pluggable look and feel, and rich set of components like tables and trees. Swing components are written entirely in Java and are platform independent, unlike AWT components which are platform dependent.

Uploaded by

Khushi Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views30 pages

Swing

Swing is the graphical user interface (GUI) toolkit that is part of Java Foundation Classes. It provides a set of lightweight components that are more advanced than the Abstract Window Toolkit (AWT) components. Some key features of Swing include its use of lightweight components, pluggable look and feel, and rich set of components like tables and trees. Swing components are written entirely in Java and are platform independent, unlike AWT components which are platform dependent.

Uploaded by

Khushi Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Java 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

 Swing class also provides a large set of components, such


as toolbars, tooltips, progress bar, password field and Timer.

 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

Difference between the AWT classes and JFC classes.


 Swing is also called as JFC‟s (Java Foundation
classes) and AWT stands for Abstract windows
toolkit.
 AWT components are called Heavyweight
component and Swings are called light weight
component because swing components sits on
the top of AWT components and do the work.
 Swing components require javax.swing package
where as AWT components require java.awt
package .
 Swings components are made in purely java
and they are platform independent whereas
AWT components are platform dependent.
 We can have different look and feel in Swing
whereas this feature is not supported in AWT.
M.S.Chahar
5

 Swing has many advanced features like JTabel, Jtabbed pane which is not

available in AWT. Also. Swing components are called "lightweight" because they

do not require a native OS object to implement their functionality. JDialog and

JFrame are heavyweight, because they do have a peer. So components like

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

"peer" is a widget provided by the operating system, such as a button object or an

entry field object.

 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

of relying on the host operating system to perform those functions.


M.S.Chahar
6

 Several consequences result from this difference


between AWT and Swing. AWT is a thin layer of code
on top of the OS, whereas Swing is much larger. Swing
also has very much richer functionality.

 Using AWT, you have to implement a lot of things


yourself, while Swing has them built in. For GUI-intensive
work, AWT feels very primitive to work with compared
to Swing. Because Swing implements GUI functionality
itself rather than relying on the host OS, it can offer a
richer environment on all platforms Java runs on.

M.S.Chahar
AWT 7

 AWT (the Abstract Window Toolkit) is the part of


Java designed for creating user interfaces and
painting graphics and images. It is a set of classes
intended to provide everything a developer
requires in order to create a graphical interface for
any Java applet or application.

 AWT components are derived from the


java.awt.Component class.

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

 MVC is a well known object-oriented user


interface design decomposition that
dates back to the late 1970s.
Components are broken down into three
parts: a model, a view, and a controller.
 Each Swing component is based on a
more modern version of this design.
Before we discuss how MVC works in
Swing, we need to understand how it was
originally designed to work.
M.S.Chahar
17

Model-view-controller architecture

M.S.Chahar
18

 Model

 The model is responsible for maintaining all aspects of the


component state.

 Ex: such values as the pressed/unpressed state of a push


button, a text component‟s character data and information
about how it is structured, etc. A model may be responsible
for indirect communication with the with the view and the
controller. By indirect we mean that the model does not
„know‟ its view and controller--it does not maintain or
retreive references to them. Instead the model will send out
notifications or broadcasts (what we know as events).

M.S.Chahar
19
 View

 The view determines the visual representation of the


component‟s model. This is a component‟s “look.”

 EX. The view displays the correct color of a component,


whether the component appears raised or lowered (in the
case of a button), and the rendering of a desired font. The
view is responsible for keeping its on-screen representation
updated and may do so upon receiving indirect messages
from the model, or direct messages from the controller.

M.S.Chahar
20
 Controller

 The controller is responsible for determining whether the


component should react to any input events from input
devices such as the keyboard or mouse. The controller is
the “feel” of the component, and it determines what
actions are performed when the component is used. The
controller can receive direct messages from the view, and
indirect messages from the model.

M.S.Chahar
21

Swing Components and


Containers Classes

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

 A JSlider is used to allow the user to select a


value from a range of numerical values by
moving a tab along a track. A JSlider is normall
oriented horizontally, but can be oriented
vertically with an extra constructor parameter.
Other parameters provide the minimum,
maximum, and starting values for the slider.
 new JSlider(int min, int max, int strt)
 new JSlider(JSlider.VERTICAL, int min, int max, int
strt)
 To respond to user actions with a JSlider, you
create a ChangeListener, implementing the
response in its StateChanged() method. The
response usually uses the JSlider's getValue()
method to get the current value of the slider.
 void addChangeListener(ChangeListener l)
 int getValue() M.S.Chahar
29

JScrollPane and JScrollbar


 Scroll bar is a component, but not a container,
a ScrollPane is a container. A ScrollPane
handles its own events and performs its own
scrolling.

M.S.Chahar
30

 Name Of Ex: Program


 Jcb.java
 Table.java
 TreeExample.java

M.S.Chahar

You might also like