ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP3114Applets Swing
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP3114

    Advanced Computer Programming

    Progress0 / 12 done
    Topics12
    1Abstract classes and Interfaces2Packages and Exception handling3Advanced issues of GUI and event handling4Applets and Swing
    5JDBC6Multithreading7Building Client/Server and implementing protocols8RMI (Remote Method Invocation)9Java Secure Socket Extension and Secure Sockets Layer (SSL)10SSL Socket and SSL Server Socket classes
    11HTTPS12Developing TCP/IP client and server with telnet
    4 min read
    Topic 4 of 12
    Topic Study Note Illustration
    04

    Applets and Swing

    An applet is a small Java program that runs inside a web browser or applet viewer .

    📘 Java API: Applets and Swing


    🔷 PART 1: APPLETS IN JAVA


    🔹 1. Definition

    An applet is a small Java program that runs inside a web browser or applet viewer.

    👉 Used for:

    • Interactive web applications
    • Animations
    • Simple games

    🔹 2. Key Features

    • Runs on client side
    • No main() method
    • Extends Applet class
    • Controlled by browser
    • Uses GUI (AWT)

    🔹 3. Life Cycle of an Applet

    Applet execution follows a sequence of methods:

    📌 Life Cycle Methods

    Method Purpose
    init() Initialize applet
    start() Start or resume
    paint(Graphics g) Display output
    stop() Pause execution
    destroy() Cleanup

    🔹 4. Applet Life Cycle Flow

    ✔ Step-by-step:

    1. Browser loads applet → init()
    2. Applet starts → start()
    3. Output displayed → paint()
    4. User leaves page → stop()
    5. Applet removed → destroy()

    🔹 5. Example of Applet

    import java.applet.Applet;
    import java.awt.Graphics;
    
    public class MyApplet extends Applet {
        public void paint(Graphics g) {
            g.drawString("Hello Applet", 50, 50);
        }
    }
    

    🔹 6. HTML to Run Applet

    <html>
    <body>
    <applet code="MyApplet.class" width="300" height="300"></applet>
    </body>
    </html>
    

    🔹 7. Advantages of Applets

    • Platform independent
    • Easy to embed in web pages
    • Secure execution environment

    🔹 8. Limitations (Important for Exams ⚠️)

    • Requires browser support (now mostly obsolete)
    • Slow performance
    • Security restrictions

    🔹 9. Diagram Description

    📊 Applet Life Cycle Diagram:

    • Flow arrows: init() → start() → paint() → stop() → destroy()

    🔷 PART 2: SWING IN JAVA


    🔹 1. Definition

    Swing is a modern Java GUI toolkit used to create rich desktop applications.

    👉 It is part of:

    • Java Foundation Classes

    🔹 2. Key Features

    • Platform independent
    • Lightweight components
    • Rich set of controls
    • Customizable look and feel

    🔹 3. Common Swing Components

    Component Description
    JFrame Main window
    JButton Button
    JLabel Text display
    JTextField Input field
    JCheckBox Selection
    JTextArea Multi-line text

    🔹 4. Basic Swing Example

    import javax.swing.*;
    
    public class SwingDemo {
        public static void main(String[] args) {
            JFrame f = new JFrame("Swing Example");
    
            JButton b = new JButton("Click");
            b.setBounds(100, 100, 100, 40);
    
            f.add(b);
            f.setSize(300, 300);
            f.setLayout(null);
            f.setVisible(true);
        }
    }
    

    🔹 5. Swing vs AWT

    Feature AWT Swing
    Components Heavyweight Lightweight
    Look OS dependent Customizable
    Flexibility Less More

    🔹 6. Event Handling in Swing

    Swing uses same event handling model:

    • Add listener to component
    • Override event method

    ✔ Example:

    import javax.swing.*;
    import java.awt.event.*;
    
    public class ButtonClick {
        public static void main(String[] args) {
            JFrame f = new JFrame();
            JButton b = new JButton("Click");
    
            b.setBounds(100, 100, 100, 40);
    
            b.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Clicked!");
                }
            });
    
            f.add(b);
            f.setSize(300, 300);
            f.setLayout(null);
            f.setVisible(true);
        }
    }
    

    🔹 7. Advanced Swing Concepts


    📌 1. Layout Managers

    • FlowLayout
    • BorderLayout
    • GridLayout

    📌 2. MVC Architecture

    Swing loosely follows:

    • Model → Data
    • View → UI
    • Controller → Logic

    📌 3. Event Dispatch Thread (EDT)

    • GUI must run on EDT
    • Avoid long operations on UI thread

    🔹 8. Important Rules

    • Always use JFrame as main container
    • Add components using add()
    • Use layout managers properly
    • Handle events using listeners

    🔷 9. Applet vs Swing

    Feature Applet Swing
    Type Web-based Desktop GUI
    Execution Browser Standalone
    Modern Use Obsolete Widely used
    Components AWT-based Advanced

    📝 Likely Exam Questions

    1. Define applet and its life cycle.
    2. Explain applet life cycle methods.
    3. Write a simple applet program.
    4. What are advantages and limitations of applets?
    5. Define Swing and its features.
    6. Difference between AWT and Swing.
    7. Write a Swing program with button.
    8. Explain event handling in Swing.
    9. What is Event Dispatch Thread?
    10. Compare Applet and Swing.

    📌 Quick Revision Summary

    🔹 Applets

    • Run in browser
    • No main()
    • Life cycle: init → start → paint → stop → destroy
    • Now mostly obsolete

    🔹 Swing

    • Used for desktop GUI
    • Lightweight and flexible
    • Components like JFrame, JButton
    • Uses event handling

    ← Previous topic 3
    Advanced issues of GUI and event handling
    Next topic 5 →
    Network Programming Concepts: JDBC

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    4mremaining
    669words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Advanced Computer Programming