Module 3 Java Revision
Module 3 Java Revision
1. JButton
2. JRadioButton
3. JCheckBox
4. JComboBox
🔹 JButton
The JButton class provides the functionality of a button.
A JButton is the Swing equivalent of a Button in AWT.
It is used to provide an interface equivalent of a common button.
JButton(Icon ic)
JButton(String str)
JButton(String str, Icon ic)
🔘 JRadio Button
A JRadioButton is the Swing equivalent of a RadioButton in AWT.
It is used to represent multiple-option, single-selection elements in a form.
This means only one option can be selected at a time among several.
To ensure single selection, multiple JRadioButton components are grouped using the
ButtonGroup class.
When radio buttons are added to a ButtonGroup, selecting one will automatically
deselect the others.
🔹 Constructors of JRadioButton
1. JRadioButton()
2. JRadioButton(String text)
3. JRadioButton(String text, boolean selected)
JCHECKBOX
A JCheckBox is the Swing equivalent of the Checkbox component in AWT.
It is sometimes called a ticker box.
Used to represent multiple option selections in a form.
Unlike JRadioButton, multiple checkboxes can be selected at the same time.
🔹 Constructors of JCheckBox
JCheckBox()
JCheckBox(String text)
JCheckBox(String text, boolean selected)
JCombo Box
The JComboBox class is used to create a combo box (drop-down list).
It allows the user to select only one item at a time from a list of options.
🔹 Constructors of JComboBox
JComboBox()
JComboBox(Object[] items)
JComboBox(Vector<?> items)
J Label
JLabel is Swing’s easiest-to-use component.
It creates a label and is typically used to display text and/or an icon.
It is a passive component, meaning it does not respond to user input (like clicking or
typing).
Useful for displaying static content like headings, descriptions, images, etc.
JLabel(Icon icon)
JLabel(String str)
JLabel(String str, Icon icon, int align)
Here,
🔹 Constructors of JTextField
1. JTextField(int cols)
2. JTextField(String str, int cols)
3. JTextField(String str)
str – the initial text in the field.
cols – number of columns (width) of the text field.
If no string is given, the text field is empty by default.
If no column count is given, the field is sized to fit the text.
J Password Field
JPasswordField is a lightweight Swing component.
It allows editing of a single line, like JTextField.
The difference is: it hides the actual characters typed, showing symbols (like ● or *)
instead.
This is mainly used for password input.
The two key features of Swing in Java are:
1. Lightweight Components
Swing components are lightweight, meaning they are written entirely in Java and do not
depend on native (platform-specific) code.
This ensures that Swing looks and behaves the same across all platforms, unlike AWT
which uses native peers.
You can use default look and feel, Windows look, Motif look, or custom styles.