:S what iz event model???
what is its use n why i shud use dis n not others?????
plzz xplain wid eg........
w8iting....:-/:?:

Dani AI

Generated

asked what the event model is and why to pick it; and started the thread. Below is a concise, practical explanation, a minimal Java example, and common pitfalls to watch for.

Think of the event model as a way to let parts of a program react when something happens (clicks, timers, incoming data) instead of the program constantly checking for changes. Producers generate small event objects; consumers register handlers that the framework calls when those events occur. This decouples who detects an input from who handles it, which makes GUIs responsive and lets servers scale without busy-waiting. Alternatives include simple polling loops, synchronous/blocking code, or actor/reactive frameworks — each has tradeoffs in complexity and latency.

Minimal Swing example (Java 8+ lambdas). Start the UI on the EDT and keep handlers short:

import javax.swing.*;
public class SmallDemo {
  public static void main(String[] args) {
    SwingUtilities.invokeLater(() -> {
      JButton b = new JButton("Click");
      b.addActionListener(e -> {
        // do quick UI work here
        System.out.println("button clicked");
      });
      JFrame f = new JFrame("Demo");
      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.add(b);
      f.pack();
      f.setVisible(true);
    });
  }
}

Practical tips: never perform long/blocking work directly in UI handlers — offload to a worker thread or SwingWorker. Always unregister listeners you no longer need (or use weak-listener helpers) to avoid memory leaks. For high-throughput IO servers prefer async NIO/selectors or a reactive library instead of spawning threads per connection. Read the Java event and Swing concurrency guidance for details and best practices: Event Handling (Swing tutorial) and Concurrency in Swing. For non-GUI async IO, see the selectors guide: Using Selectors.

Recommended Answers

All 2 Replies

Event model is based on various events and listener classes.Event model helps to respond to various external uncontrolled events.

What other models are you referring to?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.