CO
Develop Java programs to implement various Swing
Components.
Prepared by Prof. Dr. S. H. Kulkarni
Difference between AWT & Swing
AWT Swing
AWT stands for Abstract windows toolkit. Swings is advanced version of AWT.
Swing is also called as JFC’s (Java Foundation classes).
AWT components are called Heavyweight component. Swings are called light weight component because swing
components sits on the top of AWT components and do the work.
AWT components require java.awt package. Swing components require javax.swing package.
AWT components does not start with J letter. Eg Button Swing components starts with J letter eg JButton
Plug & Play is not supported in AWT. We can have different look and feel, plug & play component in
Swing.
Advanced Components are not available in AWT. Swing has many advanced features like JTabel, JTabbed pane
which is not available in AWT.
Using AWT, you have to implement a lot of things yourself. Swing has them many built in features.
Display of images with components is not possible We can display images with Components
in AWT
Prepared by Prof. Dr. S. H. Kulkarni
List of Swing Component
Jlabel
JButton, JToggleButton, JRadioButton
JTextField, JTextArea, JPasswordField
JCheckBox
JList
JComboBox
JTable
JTabbedPane
Jtree
Setting of ToolTips
JProgressBar
Prepared by Prof. Dr. S. H. Kulkarni
JLabel
Used to add description in the design.
Constructor:
JLabel()
JLabel(String s)
JLabel(String s, ImageIcon i)
Methods:
void setHorizontalAlignment(int a)
String getText()
void setText(String s)
void setIcon()
Prepared by Prof. Dr. S. H. Kulkarni
Icon
Displays image as icon with component
Constuctors:
ImageIcon(String filename)
ImageIcon(URL, ur1)
Methods:
int getIconHeight()
Int getIconWidth()
void paintIcon(Component comp, Graphics g, int x, int
y)
Prepared by Prof. Dr. S. H. Kulkarni
Program on ImageIcon
import java.awt.*;
import javax.swing.*;
/*<applet code="Jlabeldemo" width=250 height=150>
</applet>*/
public class Jlabeldemo extends JApplet
{
public void init()
{
Container con = getContentPane();
ImageIcon i1=new ImageIcon("blue flowers.jpg");
JLabel j1=new JLabel("Blue Rose", i1,JLabel.CENTER);
con.add(j1);
}
}
Prepared by Prof. Dr. S. H. Kulkarni
Thought of a Day
Prepared by Prof. Dr. S. H. Kulkarni
Output of ImageIcon Program
Prepared by Prof. Dr. S. H. Kulkarni
Hierarchy of JTextComponent classes
JTextComponent
JPasswordField JTextField JTextArea
Prepared by Prof. Dr. S. H. Kulkarni
JTextField class
Used to allow us to enter/edit a single line of text.
Constructors:
JTextField()
JTextField(Document doc, String text, int columns)
JTextField(int col)
JTextField(String text)
JTextField(String text, int columns)
Prepared by Prof. Dr. S. H. Kulkarni
JTextArea
Method or Constructor Purpose
Creates a text area. When present,
JTextArea() the String argument contains the initial
JTextArea(String) text. The int arguments specify the
JTextArea(String, int, int) desired width in columns and height in
JTextArea(int, int) rows, respectively.
void setText(String)
String getText() Sets or obtains the text displayed by the
(defined inJTextComponent) text area.
Method Purpose
void setEditable(boolean) Sets or indicates whether the user can
boolean isEditable() edit the text in the text area.
(defined inJTextComponent)
Prepared by Prof. Dr. S. H. Kulkarni
JButton
Constructor:
JButton(Icon icon)
JButton(String str)
JButton(String str, Icon icon)
Methods:
String getActionCommand( )
String getText()
void SetText(String s)
Prepared by Prof. Dr. S. H. Kulkarni
JToggleButton
A toggle button looks just like a push button, but it acts differently.
it has two states: pushed and released.
That is, when you pressed a toggle button, it stays pressed rather
than popping back up as a regular push button does.
When you press the toggle button a second time, it releases(pops
up). Therefore, each time a toggle button is pushed, it toggles
between its two states.
It is combination of JCheckBox and JRadioButton
Constructor: JToggleButton(String str)
Methods:
Object getItem( )
boolean isSelected( )
Prepared by Prof. Dr. S. H. Kulkarni
JRadioButton
Radio buttons are a group of mutually exclusive
buttons, in which only one button can be selected at
any one time.
They are supported by the JRadioButton class, which
extends JToggleButton.
JRadioButton(String str)
JRadioButton(ImageIcon i)
JRadioButton(String str, ImageIcon i)
JRadioButton(String str,ImageIcon i, boolean true)
JRadioButton(String str, boolean true)
Prepared by Prof. Dr. S. H. Kulkarni
JCheckBox
JCheckBox(String str)
JCheckBox(ImageIcon i)
JCheckBox(String str, ImageIcon i)
JCheckBox(String str,ImageIcon i, boolean true)
JCheckBox(String str, boolean true)
Prepared by Prof. Dr. S. H. Kulkarni
JList
Displays list for multiple selection as well as images
Constructors:
JList(Object[ ] items)
JList(ListModel datamodel)
JList is based on two models. The first is ListModel. This
interface defines how access to the list data is achieved.
The second model is the ListSelectionModel interface,
which defines methods that determine what list item or
items are selected.
Does not support scrolling.We must add JScrollPane
Prepared by Prof. Dr. S. H. Kulkarni
Thought of a Day
Prepared by Prof. Dr. S. H. Kulkarni
JComboBox
Swing provides a combo box (a combination of a text
field and a drop-down list) throughthe JComboBox class.
A combo box normally displays one entry, but it will also
display a drop-down list that allows a user to select a
different entry
Constuctor:
JComboBox(Object[ ] items)
JComboBox()
Methods:
void addItem(Object obj)
Prepared by Prof. Dr. S. H. Kulkarni
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
/*
<applet code="combodemo11" width=300 height=100>
</applet>
*/
public class combodemo11 extends JApplet
{
public void init()
{
Container co = getContentPane();
co.setLayout(new FlowLayout());
JComboBox jc=new JComboBox();
jc.addItem("cricket");
jc.addItem("football");
jc.addItem("hockey");
jc.addItem("tennis");
co.add(jc);
}
}
Prepared by Prof. Dr. S. H. Kulkarni
Jcombobox using Farme & array
import javax.swing.*;
public class ComboBoxExample {
JFrame f;
ComboBoxExample(){
f=new JFrame("ComboBox Example");
String country[]={"India","Aus","U.S.A","England","Newzealand"};
JComboBox cb=new JComboBox(country);
cb.setBounds(50, 50,90,20);
f.add(cb);
f.setLayout(null);
f.setSize(400,500);
f.setVisible(true);
}
public static void main(String[] args) {
new ComboBoxExample();
}
}
Prepared by Prof. Dr. S. H. Kulkarni
Thought of a Day
Prepared by Prof. Dr. S. H. Kulkarni
JTabbedPane
Constructors:
JTabbedPane()
Method:
void addTab(String s1, Component object)
The general procedure to use a tabbed pane is outlined
here:
1. Create an instance of JTabbedPane.
2. Add each tab by calling addTab( ).
3. Add the tabbed pane to the content pane.
Prepared by Prof. Dr. S. H. Kulkarni
Output of JTabbedPane
Prepared by Prof. Dr. S. H. Kulkarni
// Demonstrate JTabbedPane.
JT abbedPane
import javax.swing.*;
/*<applet code="JTabbedPaneDemo" width=400
height=100></applet>*/
public class JTabbedPaneDemo extends JApplet {
public void init() {
JTabbedPane jtp = new JTabbedPane();
jtp.addTab("Cities", new CitiesPanel());
jtp.addTab("Colors", new ColorsPanel());
jtp.addTab("Flavors", new FlavorsPanel());
add(jtp);
}}
Prepared by Prof. Dr. S. H. Kulkarni
JTabbedPane(Continues…)
// Make the panels that will be added to the tabbed pane.
class CitiesPanel extends JPanel {
public CitiesPanel() {
JButton b1 = new JButton("New York");add(b1);
JButton b2 = new JButton("London");add(b2);
JButton b3= new Jbutton(“Tokiyo”);add(b3);
}}
class ColorsPanel extends JPanel {
public ColorsPanel() {
JCheckBox cb1 = new JCheckBox("Red");add(cb1);
JCheckBox cb2 = new JCheckBox("Green");add(cb2);
JCheckBox cb3 = new JCheckBox("Blue");add(cb3);
}}
Prepared by Prof. Dr. S. H. Kulkarni
class FlavorsPanel extends JPanel {
public FlavorsPanel() {
JComboBox jcb = new JComboBox();
jcb.addItem("Vanilla");
jcb.addItem("Chocolate");
jcb.addItem("Strawberry");
add(jcb);
}}
Prepared by Prof. Dr. S. H. Kulkarni
JTree
ATree is a component that presents a hierarchical view
of data.
The user has the ability to expand or collapse
individual subtrees in this display.
constructors
JTree(Object obj[ ])
JTree(Vector<?> v)
JTree(TreeNode tn)
• JTree relies on two models: TreeModel and
TreeSelectionModel
Prepared by Prof. Dr. S. H. Kulkarni
JTREE
The TreeNode interface declares methods that obtain
information about a tree node.
For example, it is possible to obtain a reference to the
parent node or an enumeration of the child nodes. 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
Prepared by Prof. Dr. S. H. Kulkarni
DefaultMutableTreeNode
The DefaultMutableTreeNode class implements the
MutableTreeNode interface. It represents a node in a tree.
One of its constructors is shown here:
DefaultMutableTreeNode(Object obj)
Here, obj is the object to be enclosed in this tree node.
The new tree node doesn’t have a parent or children.
To create a hierarchy of tree nodes, the add( ) method of
DefaultMutableTreeNode can be used. Its signature is
shown here:
void add(MutableTreeNode child)
Prepared by Prof. Dr. S. H. Kulkarni
// Demonstrate JTree.
import java.awt.*;
import javax.swing.*;
/*<applet code="JTreeDemo" width=400 height=200>
</applet>*/
public class JTreeDemo extends JApplet {
JTree tree;
JLabel jlab;
public void init()
{
// Create top node of tree.
DefaultMutableTreeNode top = new DefaultMutableTreeNode("Options");
// Create subtree of "A".
DefaultMutableTreeNode a = new DefaultMutableTreeNode("A");
top.add(a);
DefaultMutableTreeNode a1 = new DefaultMutableTreeNode("A1");
a.add(a1);
DefaultMutableTreeNode a2 = new DefaultMutableTreeNode("A2");
a.add(a2);
Prepared by Prof. Dr. S. H. Kulkarni
// Create subtree of "B".
DefaultMutableTreeNode b = new DefaultMutableTreeNode("B");
top.add(b);
DefaultMutableTreeNode b1 = new DefaultMutableTreeNode("B1");
b.add(b1);
DefaultMutableTreeNode b2 = new DefaultMutableTreeNode("B2");
b.add(b2);
DefaultMutableTreeNode b3 = new DefaultMutableTreeNode("B3");
b.add(b3);
tree = new JTree(top); // Create the tree.
JScrollPane jsp = new JScrollPane(tree); // Add the tree to a scroll pane.
// Add the scroll pane to the content pane.
add(jsp);
// Add the label to the content pane.
jlab = new JLabel();
add(jlab, BorderLayout.SOUTH);}}
Prepared by Prof. Dr. S. H. Kulkarni
Output of JTree
Prepared by Prof. Dr. S. H. Kulkarni
Thought of a Day
Prepared by Prof. Dr. S. H. Kulkarni
JTable
JTable is a component that displays rows and columns
of data.
You can drag the cursor on column boundaries to
resize columns. You can also drag a column to a new
position.
Depending on its configuration, it is also possible to
select a row, column, or cell within the table, and to
change the data within a cell
Prepared by Prof. Dr. S. H. Kulkarni
JTable is a component that displays rows and columns of
JTable
data
JTable(Object data[ ][ ], Object colHeads[ ])
Here are the steps required to set up a simple JTable that
can be used to display data.
Steps for JTable
1. Create an instance of JTable.
2. Create a JScrollPane object, specifying the table as the
object to scroll.
3. Add the table to the scroll pane.
4. Add the scroll pane to the
Content pane.
Prepared by Prof. Dr. S. H. Kulkarni
JTable Program
import java.awt.*;
import javax.swing.*;
/*<applet code="JTableDemo" width=400 height=200>
</applet>*/
public class JTableDemo extends JApplet {
public void init() {
//Initialize column headings.
String[] colHeads = { "Name", "Extension", "ID#" };
// Initialize data.
Object[][] data = {
{ "Gail", "4567", "865" },{ "Ken", "7566", "555" },{ "Viviane", "5634", "587" },
{ "Helen", "6751", "145" }
};
Prepared by Prof. Dr. S. H. Kulkarni
JTable
Create the table.
JTable table = new JTable(data, colHeads);
// Add the table to a scroll pane.
JScrollPane jsp = new JScrollPane(table);
// Add the scroll pane to the content pane.
add(jsp);
}
}
Output
Prepared by Prof. Dr. S. H. Kulkarni
JProgressBar
import javax.swing.*;
public class ProgressBarExample extends JFrame{
JProgressBar jb;
int i=0,num=0;
ProgressBarExample(){
jb=new JProgressBar(0,2000);
jb.setBounds(40,40,160,30);
jb.setValue(0);
jb.setStringPainted(true);
add(jb);
setSize(250,150);
setLayout(null); }
Prepared by Prof. Dr. S. H. Kulkarni
JProgressBar
public void iterate(){
while(i<=2000){
jb.setValue(i);
i=i+20;
try{Thread.sleep(150);}catch(Exception e){}
}
}
public static void main(String[] args) {
ProgressBarExample m=new ProgressBarExample();
m.setVisible(true);
m.iterate();
}
Prepared by Prof. Dr. S. H. Kulkarni
JProgressBar Output
Prepared by Prof. Dr. S. H. Kulkarni
Thought of a Day
Prepared by Prof. Dr. S. H. Kulkarni
ToolTip
import javax.swing.*;
public class ToolTipExample {
public static void main(String[] args) {
JFrame f=new JFrame("Password Field Example");
//Creating PasswordField and label
JPasswordField value = new JPasswordField();
value.setBounds(100,100,100,30);
value.setToolTipText("Enter your Password");
JLabel l1=new JLabel("Password:");
l1.setBounds(20,100, 80,30);
//Adding components to frame
f.add(value); f.add(l1);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
}
Prepared by Prof. Dr. S. H. Kulkarni
ToolTip Output
Prepared by Prof. Dr. S. H. Kulkarni