Java EventQueue.postEvent(AWTEvent theEvent)

Syntax

EventQueue.postEvent(AWTEvent theEvent) has the following syntax.

public void postEvent(AWTEvent theEvent)

Example

In the following code shows how to use EventQueue.postEvent(AWTEvent theEvent) method.


import java.awt.AWTEvent;
import java.awt.Component;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.EventListener;
/*from w  w  w.  j a va2s.c  o  m*/
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

public class Main extends JPanel implements TimerListener {
  private int currentValue = 10;
    JProgressBar bar = new JProgressBar(1,100);
  public Main() {
    TimerComponent t = new TimerComponent(1000);
    t.addTimerListener(this);
    add(bar);
  }

  public void timeElapsed(TimerEvent evt) {
    currentValue += 10;
    bar.setValue(currentValue);
  }

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setTitle("Customized Event");
    frame.setSize(300, 80);
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

    Container contentPane = frame.getContentPane();
    contentPane.add(new Main());

    frame.setVisible(true);
  }
}

interface TimerListener extends EventListener {
  public void timeElapsed(TimerEvent evt);
}

class TimerComponent extends Component implements Runnable {
  private int interval;

  private TimerListener listener;

  private static EventQueue evtq;

  public TimerComponent(int i) {
    interval = i;
    Thread t = new Thread(this);
    t.start();
    evtq = Toolkit.getDefaultToolkit().getSystemEventQueue();
    enableEvents(0);
  }

  public void addTimerListener(TimerListener l) {
    listener = l;
  }

  public void run() {
    while (true) {
      try {
        Thread.sleep(interval);
      } catch (InterruptedException e) {
      }
      TimerEvent te = new TimerEvent(this);
      evtq.postEvent(te);
    }
  }

  public void processEvent(AWTEvent evt) {
    if (evt instanceof TimerEvent) {
      if (listener != null)
        listener.timeElapsed((TimerEvent) evt);
    } else
      super.processEvent(evt);
  }

}

class TimerEvent extends AWTEvent {
  public static final int TIMER_EVENT = AWTEvent.RESERVED_ID_MAX + 5555;

  public TimerEvent(TimerComponent t) {
    super(t, TIMER_EVENT);
  }
}




















Home »
  Java Tutorial »
    java.awt »




BasicStroke
BorderLayout
CardLayout
Color
Cursor
Desktop
DesktopManager
DisplayMode
EventQueue
FlowLayout
FocusTraversalPolicy
Font
FontMetrics
GradientPaint
Graphics
Graphics2D
GraphicsConfiguration
GraphicsDevice
GraphicsEnvironment
GridBagConstraints
GridBagLayout
GridLayout
Image
ItemSelectable
KeyboardFocusManager
LayoutManager
LayoutManager2
Point
Rectangle
Robot
Shape
SplashScreen
SystemColor
SystemTray
TexturePaint
TrayIcon
Toolkit
Transparency