Introduction to swing
Difference between AWT and Swing
AWT defines basic set of controls with limited graphical interface, as it translates various visual
components to platform specific equivalents. So it is platform specific and are heavyweight
Swing has advanced set of controls which are pure java and are light weight.
JApplet
ImageIcon
JLabel
JTextField
JComboBox
• Swing-based applets are similar to AWT-based applets, but with an important difference:
• A Swing applet extends JApplet rather than Applet.
• JApplet is derived from Applet. Thus, JApplet includes all of the functionality found in Applet
and adds support for Swing.
• JApplet is a top-level Swing container
• It includes the various panes like root pane, glass pane, content pane.
• All components are added to JApplet’s content pane
• Used to create label
• It can be used to display a string or an image or both
• It is a passive component
Constructors
JLabel(Icon icon)
JLabel(String str)
JLabel(String str, Icon icon, int align)
str and icon are the text and icon used for the label
align argument specifies the horizontal alignment of the text and/or icon within the dimensions of
the label, values are - values: LEFT, RIGHT, CENTER, LEADING, or TRAILING
Methods
Icon getIcon( )
String getText( )
void setIcon(Icon icon)
void setText(String str)
JTextField
JTextField allows you to edit one line of text.
It is derived from JTextComponent, which provides the basic functionality common to Swing text
components
JTextField(int cols) - number of columns in the text field
JTextField(String str, int cols) - str, string value
JTextField(String str)
Methods
int getColumns()
void setColumns()
void setFont(Font f)
JPasswordField
The object of a JPasswordField class is a text component specialized for password entry.
It allows the editing of a single line of text.
It inherits JTextField class.
JPasswordField()
JPasswordField(int columns)
JPasswordField(String text)
JPasswordField(String text, int cols)
Methods
boolean echoCharIsSet()
char getEchoChar()
char[] getPassword()
void setEchoChar(char c)
ImageIcon
The class ImageIcon is an implementation of the Icon interface that paints Icons from Images.
ImageIcon()
ImageIcon(String name)
ImageIcon(URL url)
Methods
int getIconHeight()
int getIconWidth()
JComboBox
• Swing provides a combo box through the 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
Constructors
JComboBox()
JComboBox(Object[] items)
JComboBox(Vector v)
Methods
void addItem(Object obj)
Object getSelectedItem( )
Object getItemAt(int pos)
int getItemCount()
void removeAllItems()
Swing buttons
Swing defines four types of buttons:
JButton, JToggleButton, JCheckBox, and JRadioButton.
All are subclasses of the AbstractButton class, which extends JComponent.
JButton
The JButton class provides the functionality of a push button.
JButton allows an icon, a string, or both to be associated with the push button
JButton(Icon icon)
JButton(String str)
JButton(String str, Icon icon)
When the button is pressed, an ActionEvent is generated.
Using the ActionEvent object passed to the actionPerformed( ) method of the registered
ActionListener, the action command string associated with the button can be obtained
JCheckBox
The JCheckBox class provides the functionality of a check box.
Its immediate superclass is JToggleButton, which provides support for two-state buttons
JCheckBox()
JCheckBox(String str)
JCheckBox(String str, boolean b)
JCheckBox(Icon i)
JCheckBox(Icon i, boolean b)
JCheckBox(String str, Icon i)
JCheckBox(String str, Icon i,boolean b)
When the user selects or deselects a check box, an ItemEvent is generated.
A reference to the JCheckBox that generated the event can be obtained by calling getItem( ) on the
ItemEvent passed to the itemStateChanged( ) method defined by ItemListener.
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()
JRadioButton(Icon ic)
JRadioButton(Icon ic, boolean b)
JRadioButton(String str)
JRadioButton(String str, boolean b)
JRadioButton(String str, Icon ic)
JRadioButton(String str, Icon ic, boolean b)
In order for their mutually exclusive nature to be activated, radio buttons must be configured into a
group.
Only one of the buttons in the group can be selected at any time
A button group is created by the ButtonGroup class.
ButtonGroup()
Method
void add(AbstractButton b)
A JRadioButton generates action event