Advance Java Chapter 2 Full Notes - Ur Engineering Friend
Advance Java Chapter 2 Full Notes - Ur Engineering Friend
MSBTE Diploma
Chapter 2: Swing
• Swing features
• Swing components
• Buttons
• Advance Swing Components
• MVC architecture
Please visit our Youtube Channel for more MSBTE Content. Click here !
Define swing -:
In Java, Swing is a part of Java Foundation Classes (JFC) used to create graphical user
interfaces (GUIs) for Java applications. It provides a rich set of lightweight components like
buttons, text fields, tables, trees, and more that can be used to build interactive desktop
applications.
Limitations of AWT
AWT (Abstract Window Toolkit) is the original GUI framework in Java. While it provides
basic tools to create graphical user interfaces, it has several limitations that led to the
development of Swing as a more advanced alternative. Some key limitations of AWT are:
1. Heavyweight Components:
2. Platform Dependency:
• AWT provides only a basic set of components, such as buttons, checkboxes, and text
fields. It lacks more advanced components like tables, trees, tabbed panes, etc., which
are available in Swing.
• AWT does not support customizable or pluggable look and feel, meaning the user
interface elements will always look like the default system components. Developers
have little control over how the components look, limiting the aesthetic flexibility of
applications.
6. Performance Issues:
• AWT layout managers (such as FlowLayout, BorderLayout, and GridLayout) are less
flexible and harder to use for building complex layouts compared to Swing's layout
managers. This makes designing intuitive user interfaces in AWT more difficult.
8. Threading Issues:
• AWT has threading issues because its components are not thread-safe. Developers
must take care when manipulating components across different threads, which can
lead to synchronization problems if not handled correctly.
9. Outdated Technology:
• AWT is considered an older framework, and Java developers have largely moved to
Swing or JavaFX for more modern, feature-rich GUI development. AWT is now used
mainly for backward compatibility.
Swing components are part of the Swing library in Java, which is used to create graphical
user interfaces (GUIs) for desktop applications. These components are lightweight, meaning
they are written entirely in Java and do not depend on the native system's GUI elements,
offering a consistent appearance across platforms. They are also highly customizable,
allowing developers to create modern, interactive interfaces.
1. JFrame:
o Purpose: Represents a window with title bars and borders.
o Usage: It's the main container for other components in a GUI application.
o Example: A window where you can place buttons, labels, and other
components.
2. JButton:
o Purpose: A push button that triggers an action when clicked.
o Usage: Commonly used for submitting forms, executing commands, etc.
o Example:
3. JLabel:
o Purpose: Displays a text string or an image.
o Usage: Used to add non-editable text or images to the GUI.
o Example:
4. JTextField:
o Purpose: A single-line text input field.
o Usage: Allows users to input data such as a name or email.
o Example:
5. JTextArea:
o Purpose: A multi-line area for text input or output.
o Usage: Suitable for longer text inputs, like comments or descriptions.
o Example
6. JCheckBox:
o Purpose: A checkbox that can be checked or unchecked.
o Usage: Used for selecting or deselecting options in a form.
o Example:
7. JRadioButton:
o Purpose: A button that can be selected (along with others in a group), but only
one button in the group can be selected at a time.
o Usage: Ideal for selecting one option from a set, like gender selection
(Male/Female).
o Example:
8. JComboBox:
o Purpose: A drop-down list that allows users to select one option from a set of
options.
o Usage: Commonly used for choosing options like country, language, etc.
o Example
9. JList:
o Purpose: A list of items that allows the user to select one or more items.
o Usage: Useful for presenting a list of selectable items.
o Example
10.JTable:
o Purpose: Displays tabular data in rows and columns.
o Usage: Ideal for showing structured data like a database table or spreadsheet.
o Example:
11.JScrollPane:
12.JPanel:
o Purpose: A generic container used to group other components.
o Usage: Useful for organizing layouts within a JFrame. Can hold multiple
components.
o Example:
Example of JTree:
import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
// Root node
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
// Child nodes
DefaultMutableTreeNode child1 = new DefaultMutableTreeNode("Child 1");
DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Child 2");
• A Swing component that visually represents the progress of a task, showing how
much of the task has been completed.
• Commonly used in scenarios where time-consuming operations like file downloads,
installations, or data processing are taking place.
1. Determinate: Shows the specific percentage of the task completed, useful when the
total length of the task is known.
2. Indeterminate: Indicates that the task is running, but the progress cannot be
calculated. It is often used when the duration of the task is uncertain.
// Simulate a task
try {
Thread.sleep(5000); // Task simulation for 5 seconds
} catch (InterruptedException e) {
e.printStackTrace();
}
Components of MVC:
1. Model:
o Purpose: Represents the data and the business logic of the application.
o Role: The model is responsible for handling the application's data. It responds
to requests for information and updates its state when instructed by the
controller.
o Characteristics:
▪ Encapsulates the data (e.g., user details, products).
2. View:
• Purpose: Represents the user interface and displays the data from the model.
• Role: The view is responsible for rendering the model's data in the user interface. It
gets updated when the model changes, but it does not contain business logic.
• Characteristics:
o Displays data to the user.
o Listens for user inputs like clicks, form submissions, etc.
o Updates itself based on model changes.
3. Controller: