🔄 MVC Connector Workflow
1. User interacts with the View (e.g., clicks a button).
2. The Controller receives the event and processes the logic.
3. The Model is updated based on the logic.
4. The Model notifies the View of data changes.
5. The View updates to reflect the new state.
Swing is a part of Java's Java Foundation Classes (JFC) that provides a rich set of lightweight
GUI components to create graphical user interface (GUI) applications in Java. It is defined in
the javax.swing package and allows building platform-independent window-based applications.
🔘 The Swing Buttons:
There are four types of Swing buttons:
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.
🔹 Constructors of JButton: JButton class has three constructors:
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.
🔹 Constructors of JLabel (3 Examples)
JLabel(Icon icon)
JLabel(String str)
JLabel(String str, Icon icon, int align)
Here,
str is the text for the label.
icon is the image/icon to be shown.
align sets the horizontal alignment of the text/icon inside the
label.
Alignment values can be:
LEFT, RIGHT, CENTER, LEADING, or TRAILING.
Icons are defined by the Icon interface in Swing.
The most common way to use icons is through the ImageIcon class, which implements
Icon and represents an image.
🔹 Constructor of ImageIcon ImageIcon(String filename)
J Text Field
JTextField is the simplest and most commonly used Swing text component.
It allows editing of a single line of text.
It is a subclass of JTextComponent, which provides basic text-handling features.
🔹 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.
Swing components are more flexible and customizable.
2. Pluggable Look and Feel (PLAF)
Swing allows developers to change the appearance of GUI components without
changing the underlying code.
You can use default look and feel, Windows look, Motif look, or custom styles.
This makes Swing highly adaptable to different user interface needs.