Chapter 2
Swing
Marks: 10
Introduction to Swing
Swing is set of classes which provides more
powerful and flexible components as compare to
AWT.
Swing is a part of JFC (Java Foundation Classes)
JFC are a set of GUI components which simplify
the development of desktop applications.
Build on top of AWT API and acts as
replacement of AWT API.
Swing Components are implemented using Java
and so they are platform independent.
Called lightweight components
Swing supports pluggable look and
feel.
Package : javax.swing.*
Introduction to
Swing
Difference Between AWT & Swing
No. Java AWT Java Swing
Java swing components
AWT components are
1) are platform-
platform-dependent.
independent.
AWT components are Swing components are
2)
heavyweight. lightweight.
AWT doesn't support Swing supports
3) pluggable look and pluggable look and
feel. feel.
Swing provides more
powerful
AWT provides less components such as
4) components than tables, lists,
Swing. scrollpanes,
colorchooser,
tabbedpane etc.
AWT doesn't follows
5) MVC(Model View Swing follows MVC.
Controller
Important Classes by Swing
Abstract Button
ButtonGroup
ImageIcon
JApplet
JButton
JCheckBox
JComboBox
JLabel
JRadioButton
JScrollPane
JTabbedPane
JTable
JTextField
JTree
JApplet
The JApplet class extends the Applet class
JApplet supports various “panes,” such as the
content pane, the glass pane, and the root
pane.
Components are added to the
content pane of the JApplet object.
content pane can be obtained
via Call
Container getContentPane( )
The add( ) method of Container can
be used to add a component to a
content pane.
default layout for JApplet is BorderLayout
JFrame
The JFrame class extends the Frame class
Components are added to the
content pane of the JFrame
object.
content pane can be obtained
via Call
Container getContentPane( )
The add( ) method of Container
can be used to add a component
to a content pane.
default layout for JFrame is BorderLayout
For JFrame close operation:
setDefaultCloseOperation()
Parameters:
DISPOSE_ON_CLOSE
EXIT_ON_CLOSE
JLabel
Small display area for text, image or both.
Extends JComponent.
Constructors:
JLabel()
JLabel(Icon i)
JLabel(String s)
JLabel(String s, Icon i, int align)
align argument is either LEFT, RIGHT, CENTER,
methods:
Icon getIcon( )
String getText( )
void setIcon(Icon i)
void setText(String s)
ImageIcon
Icon is small fixed size picture,
typically used to decorate
components.
ImageIcon is an implementation of
the Icon interface that paints icons
from images.
Constructors:
ImageIcon(String filename)
ImageIcon(URL url)
methods
int getIconHeight( )
int getIconWidth( )
JTextField
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.text.JTextComponent
javax.swing.JTextField
•Allows us to edit single line of text
•Constructors:
JTextField( )
JTextField(int cols)
JTextField(String s, int cols)
JTextField(String s)
•Methods:
String getText( )
void setText(String s)
AbstractButton
Swing provide Icon with Button text.
Swing buttons are subclasses of the AbstractButton
class, which extends JComponent.
AbstractButton contains many methods that allow us
to
control the behavior of buttons, check boxes, and
radio buttons
Methods to control the behavior:
void setDisabledIcon(Icon di)
void setPressedIcon(Icon pi)
void setSelectedIcon(Icon si)
void setRolloverIcon(Icon di)
Methods
String getText( )
void setText(String s)
JButton
Constructors:
JButton(Icon i)
JButton(String s)
JButton(String s, Icon i)
Methods
String getText( )
void setText(String s)
ActionEvent is generated.
ActionListener interface is needed to handle
ActionEvent.
Public actionPerformed() used to override.
JCheckBox
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JCo
mponent
javax.swing.AbstractButton
javax.swing.JToggleButton
javax.swing.JCheckBox
•Immediate superclass is JToggleButton
•Constructor:
JCheckBox()
JCheckBox(Icon i)
JCheckBox(Icon i, boolean state)
JCheckBox(String s)
JCheckBox(String s, boolean state)
JCheckBox(String s, Icon i)
JCheckBox(String s, Icon i, boolean state)
JCheckBox
Methods
String getText( )
void setText(String s)
void setSelected(boolean state)
ItemEvent is generated.
ItemListener interface is needed to handle ItemEvent.
Public itemStateChnaged() used to override.
JRadioButton
java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.AbstractButton
javax.swing.JToggleButton
javax.swing.JRadioButton
•Immediate superclass is JToggleButton
•Constructor:
JRadioButton()
JRadioButton(Icon i)
JRadioButton(Icon i, boolean state)
JRadioButton(String s)
JRadioButton(String s, boolean state)
JRadioButton(String s, Icon i)
JRadioButton(String s, Icon i, boolean state)
JRadioButton
ButtonGroup class is used to add radio button in group.
ActionEvent is generated.
ActionListener Listener interface is needed to handle
ActionEvent.
public void actionPerofrmed() used to override.
Methods
String getText( )
void setText(String s)
JComboBox
Combination of text field and drop down list.
Subclass of JComponent
Only one entry can view at a time.
Constructor:
JComboBox( )
JComboBox(Vector v)
void addItem(Object obj): Used to add object in
Combobox
JComboBox: EventHandling
ItemEvent is generated.
Implements ItemListener interface
Override: itemStateChnaged(ItemEvent ie) method
defined by ItemListener.
e
A tabbed pane is a component that appears as a
group of folders in a file cabinet.
Each folder has a title.
When a user selects a folder, its contents
become visible.
Only one of the folders may be selected at a
time.
Tabbed panes are commonly used for
setting configuration options.
Subclass of JComponent
Constructor:
JTabbedPane()
JTabbedPane(in tabPlacement)
values of tabPlacement: TOP,BOTTOM,
e
Tabs are defined via the following method :
void addTab(String str, Component comp)
Str: title of pane
Comp: component, it can be Jpanel
Steps to create JTabbedPane:
1.Create a JTabbedPane object.
2.Call addTab( ) to add a tab to the pane. Tab is
created by extending JPanel class.
3.Repeat step 2 for each tab.
4.Add the tabbed pane to the content pane of the
Japplet or JFrame
JScrollPane
A scroll pane is a component that presents a
rectangular area in which a component may be
viewed.
Horizontal and/or vertical scroll bars may be provided
if
necessary.
Subclass of JComponent
Constructor:
JScrollPane()
JScrollPane(Component comp)
JScrollPane(int vsb, int hsb)
JScrollPane(Component comp, int vsb, int hsb)
Comp: Component, vsb and hsb: Scrollbar constant
JScrollPane
vsb and hsb are constants defined by
ScrollPaneConstants
HORIZONTAL_SCROLLBAR_ALWAYS
HORIZONTAL_SCROLLBAR_AS_NEEDED
VERTICAL_SCROLLBAR_ALWAYS
VERTICAL_SCROLLBAR_AS_NEEDED
Steps to use JScrollPane:
1.Create a JComponent object.
2.Create a JScrollPane object.
3.Add Jcomponent object to JScrollPane object
4.Add the scroll pane to the content pane of the
Japplet or JFrame
.
JTree
A tree is a component that presents a hierarchical view of
data.
Trees are implemented in Swing by the JTree class, which
extends JComponent.
Constructors:
JTree(Hashtable ht)
JTree(Object obj[ ])
JTree(TreeNode tn)
JTree(Vector v)
JTree
The TreeNode interface declares methods that
obtain information about a tree node.
The MutableTreeNode interface extends TreeNode. It
declares methods that can insert and remove child nodes
or change the parent node.
The DefaultMutableTreeNode class implements the
MutableTreeNode interface. It represents a node in a
tree.
Constructor:
DefaultMutableTreeNode(Object obj)
To create a hierarchy of tree nodes, the add( ) method of
DefaultMutableTreeNode can be used.
void add(MutableTreeNode child)
JTree
A JTree object generates events when a node is
expanded or collapsed.
Tree Expansion event described by class:
TreeExpansionEvent (Package: javax.swing.event)
The addTreeExpansionListener( ) and
removeTreeExpansionListener( ) methods allow
listeners to register and unregister for these notifications.
Signature for these methods:
void addTreeExpansionListener(TreeExpansionListener tel)
void removeTreeExpansionListener(TreeExpansionListener
tel)
TreeExpansionListener interface provides the following
two methods
void treeCollapsed(TreeExpansionEvent tee)
void treeExpanded(TreeExpansionEvent tee)
JTree
The getPath( ) method of this class returns a
TreePath.
TreePath getPath( )
TreePath getPathForLocation(int x, int y):
TreePath object that encapsulates information about
the tree node that was selected by the user.
Steps to create Jtree:
1. Create a JTree object.
2. Create a JScrollPane object.
3.Add the tree to the scroll pane.
4.Add the scroll pane to the content pane of the applet.
JTable
A table is a component that displays rows and
columns of data.
Subclass of JComponent
Constructor:
JTable(Object data[ ][ ], Object colHeads[ ])
data is a two-dimensional array of the information
colHeads is a one-dimensional array with the
column headings.
Steps to create Jtable
1. Create a JTable object.
2. Create a JScrollPane object.
3.Add the table to the scroll pane.
4.Add the scroll pane to the content pane of the JApplet
or JFrame
JSeparator
It inherits JComponent class.
used to draw a line to separate widgets in
a Layout.
provides a horizontal or vertical dividing
line or empty space.
most commonly used in menus and tool
bars
Menus and toolbars provides methods that create and
add separator
For ex:
menu.add(menuitem1);
menu.add(menuitem2);
menu.addSeparator();
menu.add(menuitem3);
menu.add(menuitem4);
JSeparator
Constructors
JSeparator() : Create a horizontal separator.
JSeparator(int orientation) : Create a
horizontal or vertical separator.
Orientation is defined by SwingConstants
SwingConstants.HORIZONTAL
SwingConstants.VERTICAL
Methods:
void setOrientation(int orientation )
int getOrientation()
JToolTip
Used to display a "Tip" for a Component
Any Swing component can use the
setToolTipText() method to specify the text for
a standard tooltip.
For ex:
JButton b1=new JButton(“Submit”);
b1.setToolTipText(“Click this button to store your data”);
Or
b1.setToolTipText(“<html>Click this button <br> to
store your data </html>”);
Methods:
void setToolTipText(“String str)
String getToolTipText()
JProgressBar
visually displays the progress of some
specified task
Shows percentage of completion of
specified task
Constructor:
1. JProgressBar()
2. JProgressBar(int orientation)
SwingConstants.VERTICAL
SwingConstants.HORIZONTAL
3. JProgressBar(int min, int max) :
4. JProgressBar(int orientation, int
min, int max)
JProgressBar Methods
int getMaximum()
int getMinimum()
String getString()
void setMaximum(int n)
void setMinimum(int n)
void setValue(int n)
void setString(String s)
Void setStringPainted(boolean painted)
Introduction to MVC
Swing component follows a Model-View-
Controller architecture
Swing actually makes use of a simplified
variant of the MVC design called the model-
delegate .
100 % Java implementations of components.
Use MVC architecture.
Model represents the data
View as a visual representation of the data
Controller takes input and translates it to
changes in data
MVC
Architecture
Software design pattern for software development.
Model:
The model encompasses the state data for each
component.
Database and logic.
View:
The view refers to how you see the component
on the screen
User Interface
Controller:
dictates how the component interacts with
events
controller is responsible for handling
events on the component
Communication logic/integration logic
MVC
Architecture(Basic)
Modified MVC
Architecture
MVC Architecture :
Scrollbar example