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):
    Toolkit.getDefaultToolkit()
       .addAWTEventListener(
          new MyAWTListener(),
    	  AWTEvent.HIERARCHY_EVENT_MASK);
    
  • 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)
    {
    	System.out.println(Package.getPackage("java.awt"));
    	Frame f = new Frame("test");
    	System.out.println(Package.getPackage("java.awt"));
    }
    
    You should get
    null
    package java.awt
    
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact