How can I officially detect if AWT has been loaded? (I want to display a message in a dialog if AWT is in use, or in the console if AWT isn't in use)

Rob Wilson

The java.awt.EventQueue javadocs say:

It encapsulates asynchronous event dispatch machinery which extracts events from the queue and dispatches them by calling dispatchEvent method on this EventQueue with the event to be dispatched as an argument. The particular behavior of this machinery is implementation-dependent.
I guess this means there is no "official" way to find out whether dispatch threads are running.

A few suggestions, though:

  • The AWT thread names always seem to begin with "AWT" in practice (e.g. AWT-EventQueue-n). You could check for them by relying on this.
  • You could register an AWTEventListener with the default Toolkit. This will be triggered if the application ever dispatches an AWT event (at least on this toolkit):
          new MyAWTListener(),
  • You could check if the java.awt package is loaded. Obviously this could be true even if no events have been posted.
    public static void main(String[] args)
    	Frame f = new Frame("test");
    You should get
    package java.awt
About | Sitemap | Contact