Swing
Swing
Swing
Components And Containers
A Swing GUI consists of 2 key items: components and
containers.
2
Components
In general, Swing components are derived from the
Jcomponent class.
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.
Example: JPanel
5
JApplet
Fundamental to Swing is the JApplet class, which extends
Applet. Applets that use Swing must be subclasses of JApplet.
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)
7
Icons and Labels
In swing, icons are encapsulated by the ImageIcon class, which
paints an icon from an image pane.
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.
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)
12