ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Advanced Computer Programming
    COMP3114
    Progress0 / 12 topics
    Topics
    1. Java API: Abstract classes and Interfaces2. Packages and Exception handling3. Advanced issues of GUI and event handling4. Applets and Swing5. Network Programming Concepts: JDBC6. Multithreading7. Building Client/Server and implementing protocols8. RMI (Remote Method Invocation)9. Java Secure Socket Extension and Secure Sockets Layer (SSL)10. SSL Socket and SSL Server Socket classes11. Client and Server Authentication: HTTPS12. Developing TCP/IP client and server with telnet
    COMP3114›Applets and Swing
    Advanced Computer ProgrammingTopic 4 of 12

    Applets and Swing

    4 minread
    669words
    Beginnerlevel

    📘 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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time4 min
      Word count669
      Code examples0
      DifficultyBeginner