Chapter02 Swing
Chapter02 Swing
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.
EXIT_ON_CLOSE
JLabel
Small display area for text, image or both.
Extends JComponent.
Constructors:
JLabel()
JLabel(Icon i)
JLabel(String s)
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
•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
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)
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
•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)
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)
JScrollPane(Component comp)
.
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)
column headings.
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
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