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 PortalCOMP3114Advanced Gui Event Han…
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 3 of 12
    Topic Study Note Illustration
    03

    Advanced issues of GUI and event handling

    A GUI (Graphical User Interface) allows users to interact with programs using buttons, menus, windows, text fields , etc., instead of typing commands.

    πŸ“˜ Advanced Issues of GUI and Event Handling (Java)


    πŸ”· 1. Introduction to GUI

    A GUI (Graphical User Interface) allows users to interact with programs using buttons, menus, windows, text fields, etc., instead of typing commands.

    πŸ‘‰ Java provides GUI through:

    • AWT (Abstract Window Toolkit)
    • Swing

    πŸ”Ή AWT vs Swing

    Feature AWT Swing
    Type Heavyweight Lightweight
    Components Platform dependent Platform independent
    Look Native OS look Custom look
    Package java.awt javax.swing

    πŸ”· 2. Basic GUI Components

    Common components used in GUI:

    • Button β†’ Click action
    • Label β†’ Display text
    • TextField β†’ Input text
    • Checkbox β†’ Selection
    • Frame / Window β†’ Main container

    πŸ”Ή Example (Swing GUI)

    import javax.swing.*;
    
    public class Demo {
        public static void main(String[] args) {
            JFrame f = new JFrame("My Window");
            JButton b = new JButton("Click Me");
    
            b.setBounds(100, 100, 120, 40);
            f.add(b);
    
            f.setSize(400, 400);
            f.setLayout(null);
            f.setVisible(true);
        }
    }
    

    πŸ”· 3. Event Handling in Java


    πŸ”Ή Definition

    An event is an action like:

    • Mouse click
    • Key press
    • Button click

    Event handling is the process of responding to these events.


    πŸ”Ή Event Handling Model (Delegation Model)

    Java uses Delegation Event Model, which includes:

    πŸ“Œ Components:

    1. Event Source β†’ Generates event (e.g., Button)
    2. Event Object β†’ Contains event details
    3. Event Listener β†’ Handles event

    πŸ”Ή Flow (Step-by-step)

    1. User performs action (click button)
    2. Event is generated
    3. Event object created
    4. Listener receives event
    5. Action performed

    πŸ”Ή Diagram Description

    πŸ“Š Event Handling Diagram:

    • User β†’ Button (Event Source)
    • Arrow β†’ Event Object
    • Arrow β†’ Listener
    • Listener executes action

    πŸ”· 4. Event Classes

    Some important event classes:

    • ActionEvent β†’ Button click
    • MouseEvent β†’ Mouse actions
    • KeyEvent β†’ Keyboard input
    • WindowEvent β†’ Window actions

    πŸ”· 5. Event Listener Interfaces

    Listener Purpose
    ActionListener Button click
    MouseListener Mouse events
    KeyListener Keyboard input
    WindowListener Window events

    πŸ”· 6. Handling Events (Example)

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

    βœ” Explanation

    1. Button created
    2. Listener added using addActionListener()
    3. actionPerformed() method handles click
    4. Message displayed on click

    πŸ”· 7. Adapter Classes


    πŸ”Ή Definition

    Adapter classes provide default implementations of listener interfaces.

    πŸ‘‰ Useful when:

    • You don’t want to override all methods

    πŸ”Ή Example

    import java.awt.event.*;
    
    class MyWindow extends WindowAdapter {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    }
    

    πŸ”· 8. Inner Classes for Event Handling


    πŸ”Ή Why use Inner Classes?

    • Keeps code clean
    • Better readability
    • Direct access to outer class variables

    πŸ”Ή Example

    import javax.swing.*;
    import java.awt.event.*;
    
    public class InnerDemo {
        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("Handled using inner class");
                }
            });
    
            f.add(b);
            f.setSize(300, 300);
            f.setLayout(null);
            f.setVisible(true);
        }
    }
    

    πŸ”· 9. Advanced GUI Issues


    πŸ”Ή 1. Layout Management

    Controls arrangement of components.

    Types:

    • FlowLayout
    • BorderLayout
    • GridLayout

    πŸ”Ή 2. Threading in GUI

    • GUI should run on Event Dispatch Thread (EDT)
    • Avoid long tasks in GUI thread β†’ prevents freezing

    πŸ”Ή 3. Responsiveness

    • Use background threads for heavy tasks
    • Keep UI responsive

    πŸ”Ή 4. Exception Handling in GUI

    • Handle runtime errors properly
    • Prevent application crash

    πŸ”Ή 5. Event Delegation Efficiency

    • Avoid unnecessary listeners
    • Use proper event handling structure

    πŸ”· 10. Important Rules

    • Always register listener to component
    • One event β†’ one handler method
    • Use adapter classes when needed
    • Avoid blocking GUI thread

    πŸ“ Likely Exam Questions

    1. Define GUI and event handling.
    2. What is Delegation Event Model?
    3. Explain AWT vs Swing.
    4. What are event listeners?
    5. Write a program for button click event.
    6. What are adapter classes?
    7. Explain event handling flow with diagram.
    8. What are layout managers?
    9. Why use inner classes in event handling?
    10. What are advanced issues in GUI programming?

    πŸ“Œ Quick Revision Summary

    • GUI = graphical interaction
    • Java uses AWT & Swing
    • Event = user action
    • Event handling = responding to action
    • Model = Source β†’ Event β†’ Listener
    • Use listeners & adapter classes
    • Keep GUI responsive

    ← Previous topic 2
    Packages and Exception handling
    Next topic 4 β†’
    Applets and Swing

    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
    738words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Advanced Computer Programming