0% found this document useful (0 votes)
3 views22 pages

Java Unit-5

The document provides an overview of Java applets and Swing, detailing the concepts, life cycle, and types of applets, as well as the differences between applets and applications. It also introduces Swing as a more advanced GUI toolkit compared to AWT, highlighting its features and benefits, and explains the MVC architecture used in Swing applications. Additionally, it lists various Swing components and their purposes with example code snippets.

Uploaded by

rachanagurram136
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views22 pages

Java Unit-5

The document provides an overview of Java applets and Swing, detailing the concepts, life cycle, and types of applets, as well as the differences between applets and applications. It also introduces Swing as a more advanced GUI toolkit compared to AWT, highlighting its features and benefits, and explains the MVC architecture used in Swing applications. Additionally, it lists various Swing components and their purposes with example code snippets.

Uploaded by

rachanagurram136
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Applets and Swing

Concepts of Applets and Differences from Applications

1. Concepts of Applets:
Definition: Applets are small Java programs that are designed to run within a web
browser or applet viewer. They are used to provide interactive features on web
pages.
Execution: Applets are executed by an applet viewer or a web browser with Java
support.
Security: Applets run in a restricted environment (sandbox) for security reasons,
limiting their access to system resources.

import [Link];
import [Link];

public class SimpleApplet extends Applet {


public void paint(Graphics g) {
[Link]("Hello, Applet!", 20, 20);
}
}

2. Differences from Applications:


Execution Environment:
Applets: Run in a browser or applet viewer. They are part of a web page and
are controlled by the browser's security policy.
Applications: Run independently and directly on the Java Virtual Machine
(JVM). They have more control over system resources.
Entry Point:
Applets: Do not have a main method. They have a predefined life cycle with
methods like init() , start() , stop() , and destroy() .
Applications: Have a main method which is the entry point of the
application.
User Interface:
Applets: Typically used to create small, interactive features on web pages.
Applications: Used for standalone applications with complex user interfaces.
Security:
Applets: Run in a sandbox environment with restricted access to system
resources for security.
Applications: Have more access to system resources and are not restricted
in the same way as applets.

Life Cycle of an Applet


The life cycle of an applet involves the following methods, which are called by the browser or
applet viewer at various stages:

1. init() :
Purpose: Called once when the applet is first loaded into memory. Used for
initialization, such as setting up user interface components.
Example:

public void init() {


// Initialization code
}

2. start() :
Purpose: Called after init() to start or resume the applet. Used to start
animations or other activities.
Example:

public void start() {


// Code to start the applet
}

3. paint(Graphics g) :
Purpose: Called whenever the applet needs to be redrawn, such as when it is first
loaded or when it is resized. Used for drawing on the applet's display area.
Example:

public void paint(Graphics g) {


[Link]("Hello, Applet!", 20, 20);
}

4. stop() :
Purpose: Called when the applet is no longer visible or needs to be suspended.
Used to stop any ongoing activities like animations.
Example:
public void stop() {
// Code to stop activities
}

5. destroy() :
Purpose: Called when the applet is being unloaded from memory. Used for
cleanup, such as releasing resources.
Example:

public void destroy() {


// Cleanup code
}

Types of Applets

1. Applet :
Definition: The base class for all applets. It provides the basic functionality
needed for applets, including life cycle methods and drawing capabilities.
2. AudioClip Applet:
Definition: Applets that play audio. They use the AudioClip interface to handle
sound playback.
Example:

import [Link];
import [Link];
import [Link];

public class AudioApplet extends Applet {


private AudioClip sound;

public void init() {


URL url = getCodeBase();
sound = getAudioClip(url, "[Link]");
}

public void start() {


[Link](); // Play sound in a loop
}

public void stop() {


[Link](); // Stop sound playback
}
}

3. Applet with GUI Components:


Definition: Applets that include GUI components like buttons, text fields, and
panels.
Example:

import [Link];
import [Link];
import [Link];
import [Link];

public class GUIApplet extends Applet {


public void init() {
Button button = new Button("Click Me");
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link]("Button Clicked");
}
});
add(button);
}
}

4. Interactive Applet:
Definition: Applets that interact with the user through input events like mouse
clicks or keyboard input.
Example:

import [Link];
import [Link];
import [Link];
import [Link];

public class InteractiveApplet extends Applet implements MouseListener


{
public void init() {
addMouseListener(this);
}

public void mouseClicked(MouseEvent e) {


repaint(); // Trigger repaint on mouse click
}

public void paint(Graphics g) {


[Link]("Click anywhere", 20, 20);
}

public void mousePressed(MouseEvent e) {}


public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}

Creating Applets and Passing Parameters

1. Creating Applets:
To create an applet, extend the Applet class (or JApplet for Swing-based
applets) and override the necessary lifecycle methods ( init() , start() ,
stop() , destroy() ).
Example:

import [Link];
import [Link];

public class SimpleApplet extends Applet {


public void init() {
// Initialization code
}

public void paint(Graphics g) {


[Link]("Hello, Applet!", 20, 20);
}
}

HTML for Embedding an Applet:

<html>
<body>
<applet code="[Link]" width="300" height="200">
</applet>
</body>
</html>

2. Passing Parameters to Applets:


Applets can receive parameters from the HTML file in which they are embedded.
These parameters can be retrieved using the getParameter() method.
Example:

import [Link];
import [Link];

public class ParameterApplet extends Applet {


private String message;

public void init() {


message = getParameter("message");
if (message == null) {
message = "Default Message";
}
}

public void paint(Graphics g) {


[Link](message, 20, 20);
}
}

HTML with Parameters:

<html>
<body>
<applet code="[Link]" width="300" height="200">
<param name="message" value="Hello from Parameter!">
</applet>
</body>
</html>

Introduction to Swing and Limitations of AWT

1. Introduction to Swing:
Swing: A part of Java's standard library, Swing provides a more sophisticated set
of GUI components than AWT. It is built on top of AWT and provides a more
flexible and powerful toolkit for creating modern user interfaces.
Key Features:
Pluggable Look-and-Feel: Allows applications to have a consistent
appearance across different platforms.
Lightweight Components: Swing components are lightweight, meaning
they are not dependent on native system resources for rendering.
MVC Architecture: Swing uses the Model-View-Controller (MVC) design
pattern, which separates data (model) from user interface (view) and controls
(controller).
Basic Swing Example:

import [Link];
import [Link];
import [Link];
import [Link];

public class SwingExample {


public static void main(String[] args) {
JFrame frame = new JFrame("Swing Example");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](new FlowLayout());

JButton button = new JButton("Click Me");


[Link](button);

[Link](300, 200);
[Link](true);
}
}

2. Limitations of AWT:

Heavyweight Components: AWT components are heavyweight, meaning they


rely on native system resources for rendering. This can result in inconsistent look
and feel across different platforms.
Limited Customization: AWT provides limited options for customizing the
appearance of components compared to Swing.
Look-and-Feel: AWT does not support pluggable look-and-feel, meaning that
components appear with the native OS appearance.
Event Handling: AWT's event handling model is less flexible and more
cumbersome compared to Swing's event handling.
Deprecated Components: Some AWT components are considered outdated and
have been replaced by newer Swing components.

Comparison Example:
AWT Frame Example:

import [Link];
import [Link];

public class AWTExample {


public static void main(String[] args) {
Frame frame = new Frame("AWT Example");
[Link](new FlowLayout());
Button button = new Button("Click Me");
[Link](button);

[Link](300, 200);
[Link](true);
}
}

Swing Frame Example:

import [Link];
import [Link];
import [Link];
import [Link];

public class SwingExample {


public static void main(String[] args) {
JFrame frame = new JFrame("Swing Example");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](new FlowLayout());

JButton button = new JButton("Click Me");


[Link](button);

[Link](300, 200);
[Link](true);
}
}

Swing Benefits:
Better appearance and consistency across platforms.
Greater flexibility and customization options.
Richer set of GUI components.

MVC Architecture and Components in Swing


MVC Architecture
Model-View-Controller (MVC) is a design pattern used to separate the concerns of an
application, making it more modular and easier to manage. In Swing, MVC helps in
organizing the application into three distinct components:

1. Model:
Purpose: Represents the data and business logic of the application. It manages
the data and notifies the view about any changes in the data.
Example: A class that manages the data of a data table or a class that handles
the application's business logic.
In Swing: Swing components like JTable and JList use models to manage and
represent their data.
2. View:
Purpose: Represents the graphical user interface (GUI) of the application. It
displays the data and sends user commands to the controller.
Example: GUI components like buttons, labels, and text fields.
In Swing: Swing provides a wide range of view components like JButton ,
JLabel , JTextField , etc., to display and interact with the data.
3. Controller:
Purpose: Acts as an intermediary between the model and the view. It receives
user input from the view, processes it, and updates the model and/or the view
accordingly.
Example: Event listeners like ActionListener and MouseListener that handle
user interactions.
In Swing: Controllers are often implemented using event listeners. For example,
an ActionListener for a button will handle the button click event and update the
model or view as needed.

Components in Swing

Swing provides a rich set of components for building modern graphical user interfaces. Here
are some key Swing components:

1. JFrame:
Purpose: Represents a top-level window with standard decorations (title bar,
border, etc.).
Example:

import [Link];

public class MyFrame {


public static void main(String[] args) {
JFrame frame = new JFrame("My Frame");
[Link](400, 300);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}
2. JPanel:
Purpose: A container used to group and organize other components.
Example:

import [Link];
import [Link];
import [Link];

public class PanelExample {


public static void main(String[] args) {
JFrame frame = new JFrame("Panel Example");
JPanel panel = new JPanel();
[Link](new JButton("Button 1"));
[Link](new JButton("Button 2"));
[Link](panel);
[Link](300, 200);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}

3. JButton:
Purpose: Represents a push button that can trigger an action when clicked.
Example:

import [Link];
import [Link];
import [Link];
import [Link];

public class ButtonExample {


public static void main(String[] args) {
JFrame frame = new JFrame("Button Example");
JButton button = new JButton("Click Me");
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link]("Button Clicked");
}
});
[Link](button);
[Link](200, 100);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}
4. JLabel:
Purpose: Displays a non-editable text or image.
Example:

import [Link];
import [Link];

public class LabelExample {


public static void main(String[] args) {
JFrame frame = new JFrame("Label Example");
JLabel label = new JLabel("Hello, Swing!");
[Link](label);
[Link](200, 100);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}

5. JTextField:
Purpose: Allows the user to enter a single line of text.
Example:

import [Link];
import [Link];

public class TextFieldExample {


public static void main(String[] args) {
JFrame frame = new JFrame("TextField Example");
JTextField textField = new JTextField("Enter text here");
[Link](textField);
[Link](300, 100);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}

6. JTextArea:
Purpose: Allows the user to enter multiple lines of text.
Example:

import [Link];
import [Link];

public class TextAreaExample {


public static void main(String[] args) {
JFrame frame = new JFrame("TextArea Example");
JTextArea textArea = new JTextArea("Enter multiple lines of
text");
[Link](textArea);
[Link](300, 200);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}

7. JCheckBox:
Purpose: Allows the user to select or deselect an option.
Example:

import [Link];
import [Link];

public class CheckBoxExample {


public static void main(String[] args) {
JFrame frame = new JFrame("CheckBox Example");
JCheckBox checkBox = new JCheckBox("Check Me");
[Link](checkBox);
[Link](200, 100);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}

8. JRadioButton:
Purpose: Represents a radio button that allows the user to select one option from
a group.
Example:

import [Link];
import [Link];
import [Link];
import [Link];

public class RadioButtonExample {


public static void main(String[] args) {
JFrame frame = new JFrame("RadioButton Example");
JPanel panel = new JPanel();
JRadioButton option1 = new JRadioButton("Option 1");
JRadioButton option2 = new JRadioButton("Option 2");
ButtonGroup group = new ButtonGroup();
[Link](option1);
[Link](option2);
[Link](option1);
[Link](option2);
[Link](panel);
[Link](300, 200);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}

9. JList:
Purpose: Displays a list of items that can be selected.
Example:

import [Link];
import [Link];
import [Link];

public class ListExample {


public static void main(String[] args) {
JFrame frame = new JFrame("List Example");
String[] data = {"Item 1", "Item 2", "Item 3"};
JList<String> list = new JList<>(data);
[Link](new JScrollPane(list));
[Link](300, 200);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}

10. JTable:
Purpose: Displays tabular data.
Example:

import [Link];
import [Link];
import [Link];

public class TableExample {


public static void main(String[] args) {
JFrame frame = new JFrame("Table Example");
String[] columns = {"Name", "Age"};
Object[][] data = {
{"Alice", 30},
{"Bob", 25}
};
JTable table = new JTable(data, columns);
[Link](new JScrollPane(table));
[Link](300, 200);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}

Swing components are more advanced and provide a greater degree of customization
compared to AWT components. They are also designed to be more portable and consistent
across different platforms.

Exploring Swing Components


Swing offers a rich set of components for building graphical user interfaces in Java. Here’s
an overview of some key Swing components and how to use them:

JApplet

Purpose: Provides the applet functionality for Swing-based applets.


Usage: Although applets are largely obsolete, JApplet was used for embedding Swing
components into web pages.
Example:

import [Link];
import [Link];
import [Link];

public class SwingApplet extends JApplet {


public void init() {
JPanel panel = new JPanel();
JButton button = new JButton("Click Me");
[Link](button);
add(panel);
}
}

JFrame

Purpose: Represents a top-level window with standard decorations (title bar, border,
etc.).
Usage: Used as the main window of a Swing application.
Example:

import [Link];
import [Link];

public class JFrameExample {


public static void main(String[] args) {
JFrame frame = new JFrame("JFrame Example");
[Link](JFrame.EXIT_ON_CLOSE);
[Link](300, 200);
JButton button = new JButton("Click Me");
[Link](button);
[Link](true);
}
}

JComponent

Purpose: Base class for all Swing components that have a graphical representation. It
provides basic functionality and properties for all Swing components.
Usage: You typically don’t use JComponent directly but extend it to create custom
components.
Example:

import [Link];
import [Link];

public class CustomComponent extends JComponent {


@Override
protected void paintComponent(Graphics g) {
[Link](g);
[Link]("Custom Component", 20, 20);
}
}

Icons

Purpose: Used to represent images or graphics in Swing components.


Usage: Commonly used with buttons and labels to display images.
Example:
import [Link];
import [Link];
import [Link];

public class IconExample {


public static void main(String[] args) {
JFrame frame = new JFrame("Icon Example");
ImageIcon icon = new ImageIcon("path/to/[Link]");
JButton button = new JButton(icon);
[Link](button);
[Link](200, 200);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}

JLabel

Purpose: Displays text or an image.


Usage: Used for displaying static text or images.
Example:

import [Link];
import [Link];

public class JLabelExample {


public static void main(String[] args) {
JFrame frame = new JFrame("JLabel Example");
JLabel label = new JLabel("Hello, JLabel!");
[Link](label);
[Link](300, 100);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}

JTextField

Purpose: Allows the user to input a single line of text.


Usage: Used for accepting user input.
Example:
import [Link];
import [Link];

public class JTextFieldExample {


public static void main(String[] args) {
JFrame frame = new JFrame("JTextField Example");
JTextField textField = new JTextField("Type here");
[Link](textField);
[Link](300, 100);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}

JButton

Purpose: Represents a push button that can trigger an action when clicked.
Usage: Commonly used to perform actions when the user interacts with it.
Example:

import [Link];
import [Link];
import [Link];
import [Link];

public class JButtonExample {


public static void main(String[] args) {
JFrame frame = new JFrame("JButton Example");
JButton button = new JButton("Click Me");
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
[Link]("Button Clicked");
}
});
[Link](button);
[Link](200, 100);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}

JCheckBox
Purpose: Allows the user to select or deselect an option.
Usage: Used for options where multiple selections are allowed.
Example:

import [Link];
import [Link];

public class JCheckBoxExample {


public static void main(String[] args) {
JFrame frame = new JFrame("JCheckBox Example");
JCheckBox checkBox = new JCheckBox("Check Me");
[Link](checkBox);
[Link](200, 100);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}

JRadioButton

Purpose: Allows the user to select one option from a group of options.
Usage: Used when only one option should be selected from a group.
Example:

import [Link];
import [Link];
import [Link];
import [Link];

public class JRadioButtonExample {


public static void main(String[] args) {
JFrame frame = new JFrame("JRadioButton Example");
JPanel panel = new JPanel();
JRadioButton rb1 = new JRadioButton("Option 1");
JRadioButton rb2 = new JRadioButton("Option 2");
ButtonGroup group = new ButtonGroup();
[Link](rb1);
[Link](rb2);
[Link](rb1);
[Link](rb2);
[Link](panel);
[Link](300, 100);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}

JComboBox

Purpose: Allows the user to choose from a drop-down list of options.


Usage: Used for selecting one option from a predefined list.
Example:

import [Link];
import [Link];

public class JComboBoxExample {


public static void main(String[] args) {
JFrame frame = new JFrame("JComboBox Example");
String[] options = {"Option 1", "Option 2", "Option 3"};
JComboBox<String> comboBox = new JComboBox<>(options);
[Link](comboBox);
[Link](200, 100);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}

JTabbedPane

Purpose: Allows the user to switch between different panels using tabs.
Usage: Used for organizing multiple panels within a single window.
Example:

import [Link];
import [Link];
import [Link];
import [Link];

public class JTabbedPaneExample {


public static void main(String[] args) {
JFrame frame = new JFrame("JTabbedPane Example");
JTabbedPane tabbedPane = new JTabbedPane();
JPanel panel1 = new JPanel();
[Link](new JLabel("Panel 1"));
JPanel panel2 = new JPanel();
[Link](new JLabel("Panel 2"));
[Link]("Tab 1", panel1);
[Link]("Tab 2", panel2);
[Link](tabbedPane);
[Link](300, 200);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}

JScrollPane

Purpose: Provides a scrollable view of another component.


Usage: Used to make components like JTextArea or JTable scrollable.
Example:

import [Link];
import [Link];
import [Link];

public class JScrollPaneExample {


public static void main(String[] args) {
JFrame frame = new JFrame("JScrollPane Example");
JTextArea textArea = new JTextArea(10, 30);
JScrollPane scrollPane = new JScrollPane(textArea);
[Link](scrollPane);
[Link](400, 300);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}

JTree

Purpose: Displays hierarchical data in a tree structure.


Usage: Used for representing data in a tree format, such as file systems or
organizational structures.
Example:

import [Link];
import [Link];
import [Link];
public class JTreeExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JTree Example");
DefaultMutableTreeNode root = new
DefaultMutableTreeNode("Root");
DefaultMutableTreeNode child1 = new
DefaultMutableTreeNode("Child

1");
DefaultMutableTreeNode child2 = new DefaultMutableTreeNode("Child 2");
[Link](child1);
[Link](child2);
JTree tree = new JTree(root);
[Link](tree);
[Link](300, 200);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}
```

JTable

Purpose: Displays tabular data in a grid.


Usage: Used to show data in a table format, allowing for easy data manipulation and
viewing.
Example:

import [Link];
import [Link];
import [Link];

public class JTableExample {


public static void main(String[] args) {
JFrame frame = new JFrame("JTable Example");
String[] columns = {"Name", "Age"};
Object[][] data = {
{"Alice", 30},
{"Bob", 25}
};
JTable table = new JTable(data, columns);
[Link](new JScrollPane(table));
[Link](300, 200);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}
}

You might also like