Professional Documents
Culture Documents
Archana Gopnarayan
Lecturer , Department of Information Technology (NBA Accrediated)
Vidyalankar Polytechnic
Swing
• Swing is a set of classes that provides more powerful and flexible
components than are possible with the AWT.
• In addition to the familiar components, such as buttons, check boxes, and
labels, Swing supplies several exciting additions, including tabbed panes,
scroll panes, trees, and tables. Even familiar components such as buttons
have more capabilities in Swing.
• a button may have both an image and a text string associated with it. Also,
the image can be changed as the state of the button changes.
• Swing Components Are Lightweight
• Swing Supports a Pluggable Look and Feel
Swing Classes
AbstractButton Abstract superclass for Swing buttons.
ButtonGroup Encapsulates a mutually exclusive set of buttons.
ImageIcon Encapsulates an icon.
JApplet The Swing version of Applet.
JButton The Swing push button class.
JCheckBox The Swing check box class.
JComboBox Encapsulates a combo box (an combination of a
drop-down list and text field).
JLabel The Swing version of a label.
JRadioButton The Swing version of a radio button.
JScrollPane Encapsulates a scrollable window.
JTabbedPane Encapsulates a tabbed window.
JTable Encapsulates a table-based control.
JTextField The Swing version of a text field.
JTree Encapsulates a tree-based control.
JApplet
• Fundamental to Swing is the JApplet class, which extends Applet. JApplet is rich
with functionality that is not found in Applet.
• When adding a component to an instance of JApplet, do not invoke the add( )
method of the applet. Instead, call add( ) for the content pane of the Japplet object.
• The content pane can be obtained via the method shown here:
getContentPane( )
Icons and Labels
• In Swing, icons are encapsulated by the ImageIcon class, which paints an icon
from an image.
Two of its constructors are shown here:
ImageIcon(String filename)
ImageIcon(URL url)
Methods
int getIconHeight( )
int getIconWidth( )
• Swing labels are instances of the JLabel class, which extends JComponent. It can
display text and/or an icon.
Constructors are shown here:
• JLabel(Icon i)
• JLabel(String s)
• JLabel(String s, Icon i, int align)
The align argument is either LEFT, RIGHT, CENTER, LEADING, or TRAILING.
methods:
• Icon getIcon( )
• String getText( )
• void setIcon(Icon i)
• void setText(String s)
import java.awt.*;
import javax.swing.*;
/*<applet code="JLabelDemo" width=250 height=150>
</applet>*/
public class JLabelDemo extends JApplet
{
public void init()
{
Container contentPane = getContentPane();
ImageIcon ii = new ImageIcon("rose.jpeg");
JLabel jl = new JLabel(ii);
contentPane.add(jl);
}
}
Text Fields
• The Swing text field is encapsulated by the JTextComponent class, which
extends JComponent.
• JTextField, which allows us to edit one line of text.
• constructors are shown here:
JTextField( )
JTextField(int cols)
JTextField(String s, int cols)
JTextField(String s)
• Construct
JToggleButton()
JToggleButton(Icon i)
JToggleButton(Icon i, boolean selected)
JToggleButton(String text)
JToggleButton(String text, boolean selected)
JToggleButton(String text, Icon i)
JToggleButton(String text, Icon i, boolean selected)
Check Box
• The JCheckBox class, which provides the functionality of a check box, is a concrete
implementation of AbstractButton. Its immediate superclass is JToggleButton,
which provides support for two-state buttons.
Some of its constructors are shown here:
• 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)
Radio Buttons
• Radio buttons are supported by the JRadioButton class, which is a concrete
implementation of AbstractButton. Its immediate superclass is JToggleButton,
which provides support for two-state buttons.
• Some of its constructors are shown here:
• 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)
ButtonGroup
• Radio buttons must be configured into a group. Only one of the buttons in that group
can be selected at any time.
• For example, if a user presses a radio button that is in a group, any previously
selected button in that group is automatically deselected.
• The ButtonGroup class is instantiated to create a button group. Its default constructo
• is invoked for this purpose.
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
/*<applet code="JRadioButtonDemo" width=300 height=50></applet>*/
public class JRadioButtonDemo extends JApplet implements ActionListener
{
JLabel jlab;
JRadioButton b1,b2,b3;
Panel p;
public void init()
{
Container c=getContentPane();
p=new Panel();
b1 = new JRadioButton("Information Technology");
b2 = new JRadioButton("Computer Engineering");
b3 = new JRadioButton("Electronics");
p.add(b1);
p.add(b2);
p.add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
ButtonGroup bg = new ButtonGroup();
bg.add(b1);
bg.add(b2);
bg.add(b3);
jlab = new JLabel("Select One");
p.add(jlab);
c.add(p);
}
public void actionPerformed(ActionEvent ae)
{
jlab.setText("You selected " + ae.getActionCommand());
}
}
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
/*<applet code="JRadioButtoncolor" width=300 height=50>
</applet>*/
public class JRadioButtoncolor extends JApplet implements ActionListener
{
JLabel jlab;
JRadioButton b1,b2,b3;
Panel p;
public void init()
{
Container c=getContentPane();
p=new Panel();
b1 = new JRadioButton("Red");
b2 = new JRadioButton("Green");
b3 = new JRadioButton("Blue");
p.add(b1);
p.add(b2);
p.add(b3);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
ButtonGroup bg = new ButtonGroup();
bg.add(b1);
bg.add(b2);
bg.add(b3);
c.add(p);
}
public void actionPerformed(ActionEvent ae)
{
if(b1.isSelected())
p.setBackground(Color.RED);
else if(b2.isSelected())
p.setBackground(Color.GREEN);
else
p.setBackground(Color.BLUE);
}
}
Combo Boxes
• Swing provides a combo box (a combination of a text field and a drop-down list)
• through the JComboBox class, which extends JComponent.
• A combo box normallydisplays one entry. However, it can also display a drop-down
list that allows a user to select a different entry.
• JComboBox’s constructors are shown here:
JComboBox( )
JComboBox(Vector v)
JComboBox(Object[ ] items)
public ColorsPanel()
{
setBackground(Color.PINK);
JCheckBox cb1 = new JCheckBox("Red");
add(cb1);
JCheckBox cb2 = new JCheckBox("Green");
add(cb2);
JCheckBox cb3 = new JCheckBox("Blue");
add(cb3);
}
}
Tabbed Panes
class FlavorsPanel extends JPanel
{
public FlavorsPanel()
{
setBackground(Color.MAGENTA);
JComboBox jcb = new JComboBox();
jcb.addItem("Vanilla");
jcb.addItem("Chocolate");
jcb.addItem("Strawberry");
add(jcb);
}
}
Tree
• 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.
• Trees are implemented in Swing by the JTree class.
constructors are shown here:
JTree(Object obj[ ])
JTree(Vector<?> v)
JTree(TreeNode tn)
Construct:
JTable(Object data[ ][ ], Object colHeads[ ])
JTable
import java.awt.*;
import javax.swing.*;
/*
<applet code="JTableDemo" width=400 height=200>
</applet>
*/
public class JTableDemo extends JApplet {
public void init()
{
String[] colHeads = { "Name", "Extension", "ID#" };
Object[][] data = {
{ "Gail", "4567", "865" },
{ "Ken", "7566", "555" },
{ "Viviane", "5634", "587" },
{ "Melanie", "7345", "922" },
};
JTable
JTable table = new JTable(data, colHeads);
JScrollPane jsp = new JScrollPane(table);
add(jsp);
}
}
ToolTip
• We can add tooltip text to almost all the components of Java Swing by using
the setToolTipText(String s).
• This method sets the tooltip of the component to the specified string s .
• when the cursor enters the boundary of that component a popup appears
and text is displayed .
Methods used:
• getToolTipText() : returns the tooltip text for that component .
• setToolTipText(String s) : sets the tooltip text for the component .
Progress Bar
• JProgressBar visually displays the progress of some specified task.
• JProgressBar shows the percentage of completion of specified task.
• The progress bar fills up as the task reaches it completion. In addition to show the percentage
of completion of task.
• Constructors of JProgressBar :
• JProgressBar() : creates an progress bar with no text on it;
• JProgressBar(int orientation) : creates an progress bar with a specified orientation.
• if SwingConstants.VERTICAL is passed as argument a vertical progress bar is created, if
SwingConstants.HORIZONTAL is passed as argument a horizontal progress bar is created.
• JProgressBar(int min, int max) : creates an progress bar with specified minimum and
maximum value.
• JProgressBar(int orientation, int min, int max) : creates an progress bar with specified
minimum and maximum value and a specified orientation.
Commonly used methods of JProgressBar are :