The Tour of Swing
The Tour of Swing
Japplet
Ø Icons and Labels
Ø Text Fields
Ø Buttons
Ø Combo Boxes
Ø Checkboxes
Ø Tabbed Panes
Ø Scroll Panes
Ø Exploring the Swings
Ø Tables
Ø Tree
INTRODUCTION
Swing is a set of classes this 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. For example, 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. Unlike AWT components, Swing components are
not implemented by platform-specific code.
The Swing component classes that are shown below:
Class Description
Ø AbstractButton:- Abstract super-class 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 (a 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
Java Swing class hierarchy
SWING FEATURES
Borders
Ø We can draw borders in many different styles around
components using the setborder( ) method
Graphics Debugging
Ø We can use setDebuggingGraphicsOptions method to set up
graphics debugging which means among the other things,
that you can watch each line as its drawn and make it
flash.
Easy mouseless operation
Ø It is easy to connect keystrokes to components.
Tooltips
Ø We can use the setToolTipText method of JComponent to
give components a tooltip, one of those small windows that
appear when the mouse hovers over a component and gives
explanatory text.
……..CONTINUE
Easy Scrolling
Ø We can connect scrolling to various components-something that
was impossible in AWT.
Pluggable look and feel
Ø We can set the appearance of applets and applications to one of
three standard looks. Windows, Motif (Unix) or Metal
(Standard swing look).
New Layout Managers
Ø Swing introduces the BoxLayout and OverlayLayout layout
JAPPLET
Fundamental to Swing is the JApplet class,
which extends Applet. Applets that use Swing
must be subclasses of JApplet.
JApplet is rich with functionality that is not
found in Applet. For example, JApplet supports
various “panes,” such as the content pane, the
glass pane, and the root pane.
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
theJApplet object. method shown here:
Container getContentPane( )
Its form is shown here:
void add(comp)
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)
implements the Icon interface that declares the methods shown here:
Method Description
int getIconHeight( ) Returns the height of the icon in pixels.
int getIconWidth( ) Returns the width of the icon in pixels.
void paintIcon(Component comp, Paints the icon at Graphics g, int x, int y)
position x,y on the graphics context
g.additional information about the paint
operation can be provided in comp.
Swing labels are instances of the JLabel class, which extends JComponent. It can
display text and/or an icon. Some of its constructors are
JLabel(Icon i)
Label(String s)
JLabel(String s, Icon i, int align)
The icon and text associated with the label can be read
and written by the following 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("IC.jpg");
JLabel jl = new JLabel("IC", ii, JLabel.CENTER);
contentPane.add(jl);}}
TEXT FIELDS
The Swing text field is encapsulated by the JTextComponent
class, which extends JComponent.
It provides functionality that is common to Swing text
components.
One of its subclasses is JTextField, which allows us to edit one
line of text. Some of its constructors are shown here:
JTextField( )
JTextField(int cols)
JTextField(String s, int cols)
JTextField(String s)
import java.awt.*;
import javax.swing.*;
/*<applet code="JTextFieldDemo" width=300 height=50>
</applet>*/
public class JTextFieldDemo extends JApplet{
JTextField jtf;
public void init(){
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
jtf = new JTextField(15);
contentPane.add(jtf);}}
BUTTONS
Swing buttons provide features that are not found in the
Button class defined by the AWT. For example, we can
associate an icon with a Swing button. 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 box and radio buttons.
For example, we can define different icons that are
displayed for the component when it is disabled, pressed, or
selected. Another icon can be used as rollover icon, which is
displayed when the mouse is positioned over that
component. The following are the methods
void setDisabledIcon(Icon di)
void setPressedIcon(Icon pi)
void setSelectedIcon(Icon si) void setRolloverIcon(Icon ri)
JBUTTON CLASS
DefaultMutableTreeNode
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)
Here, child is a mutable tree node that is to be added as a
child to the current node.
Tree expansion events are described by the class
TreeExpansionEvent in the javax.swing.event package.
The getPath( ) method of this class returns a TreePath
object that describes the path to the changed node. e:
ddTreePath getPath( )
The TreeExpansionListener interface provides the
following two method:
void treeCollapsed(TreeExpansionEvent tee)
void treeExpanded(TreeExpansionEvent tee)
The steps that we should follow to use a tree in an applet:
1. Create a JTree object.
b.add(b1); jtf.setText(""); }}
DefaultMutableTreeNode b2=new
DefaultMutableTreeNode("B2");
TABLES
A table is a component that displays rows and columns of data. We can drag the cursor on
column boundaries to resize columns. We can also drag a column to a new position. Tables
are implemented by the JTable class, which extends JComponent. One of its constructors
is shown here:
JTable(Object data[ ][ ], Object colHeads[ ]):-data is a two-dimensional array of the
information to be presented, and colHeads is a one-dimensional array with the column
headings.
JTable(int numRows, int numColumns):- The ‘numRows’ and ‘numColumns’ are values with
which the table is to be created.
JTable(Vector rowData, Vector columnData):-The ‘rowData’ and ‘columnData’ are the vector
values by which the table is constructed.
Here are the steps for using a table in an applet:
1. Create a JTable object.
2. Create a JScrollPane object. (The arguments to the constructor specify the table and the
policies for vertical and horizontal scroll bars.)
3. Add the table to the scroll pane.
4. Add the scroll pane to the content pane of the applet
import java.awt.*;
import javax.swing.*;
public class JTableDemo extends JApplet{
public void init(){
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
final String[] colHeads = { "Name", "Phone", "Fax" };
final Object[][] data = {{ "Pramod", "4567", "8675" },{ "Tausif", "7566", "5555" },
{ "Nitin", "5634", "5887" },{ "Amol", "7345", "9222" },{ "Vijai", "1237", "3333" },
{ "Ranie", "5656", "3144" },{ "Mangesh", "5672", "2176" },{ "Suhail", "6741", "4244" },
{ "Nilofer", "9023", "5159" },{ "Jinnie", "1134", "5332" },{ "Heena", "5689", "1212" },
{ "Saurav", "9030", "1313" },{ "Raman", "6751", "1415" } };
JTable table = new JTable(data, colHeads);
int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
JScrollPane jsp = new JScrollPane(table, v, h);
contentPane.add(jsp, BorderLayout.CENTER);
}}