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›Advanced issues of GUI and event handling
    Advanced Computer ProgrammingTopic 3 of 12

    Advanced issues of GUI and event handling

    4 minread
    738words
    Beginnerlevel

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

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