0% found this document useful (0 votes)
8 views

Swing

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Swing

Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

Chapter 15 : Exploring

Swing
Components And Containers
A Swing GUI consists of 2 key items: components and
containers.

A component is an individual visual control, such as a push


button or a slider.

A container holds a group of components.

Thus, a container is a special type of component that is


designed to hold other components.

Furthermore, in order for a component to be displayed, it must


be held within a container.

Thus, all Swing GUIs will have atleast one container.

2
Components
In general, Swing components are derived from the
Jcomponent class.

Jcomponent provides the functionality that is common to all


components.

For example, JComponent supports the pluggable look and feel.

JComponent inherits the AWT classes Container and


Component.

Thus, a Swing component is built on and compatible with an


AWT component.

3
The Swing Component
Classes
JButton JTree

JCheckBox

JComboBox

JLabel

JList

JRadioButton

JScrollPane

JTabbedPane

JTable

JTextField

JToggleButton
4
Containers
Swing defines two types of containers.

The first are top-level containers: JFrame, JApplet, JWindow and


JDialog.

These containers do not inherit JComponent. They do, however,


inherit the AWT classes Component and Container.

The 2nd type of containers supported by Swing are lightweight


containers. Lightweight containers do inherit JComponent.

Example: JPanel

5
JApplet
Fundamental to Swing is the JApplet class, which extends
Applet. Applets that use Swing must be subclasses of JApplet.

JApplet is rich in 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 the JApplet object.

The content pane can be obtained via the method:

Container getContentPane()

6
JApplet
The add() method of Container can be used to add a
component to a content pane. Its form is shown here:

void add(comp)

Here, comp is the component to be added to the content pane.

7
Icons and Labels
In swing, icons are encapsulated by the ImageIcon class, which
paints an icon from an image pane.

Two of its constructors are shown here:

ImageIcon(String filename)
ImageIcon(URL URL)

The first form uses the image in the file named filename. The second
form uses the image in the resource identified by url.

The ImageIcon class implements the Icon interface that declares


the methods:

int getIconHeight()
int getIconWidth()
void paintIcon(Component comp, Graphics g, int x, int y)
8
Icons and Labels

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 Paints the icon at position x, y on the
comp, Graphics g, int x, int graphics context g. Additional
y) information about the paint operation
can be provided in comp.

9
Icons and Labels
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)

Here, s and i are the text and icon used for the label. The align
argument is either LEFT, RIGHT, CENTER, LEADING, or
TRAILING. These constants are defined in the
SwingConstants interface, along with several others used by
the Swing classes.

10
Icons and Labels
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)

Here, i and s are the icon and text, respectively.

The example given in “File 1” illustrates how to create and


display a label containing both an icon and a string. The applet
begins by getting its content pane. Next, an ImageIcon object is
created for the file fig.gif. This is used as the second argument
to the Jlabel constructor. The first and last arguments for the
JLabel constructor are the label text and the alignment. Finally,
the label is added to the content pane.
11
Text Fields

Refer Chapter 29 and 30 of the book “The Complete Reference


– 7th Edition” for the rest of the topics related to Swings.

12

You might also like