The Source for Java Technology Collaboration
User: Password:



Artem Ananiev

Artem Ananiev's Blog

Headless toolkit basics

Posted by ixmal on April 04, 2006 at 10:15 AM | Comments (5)

Toolkit

java.awt.Toolkit is an abstract superclass of all actual implementations of the Abstract Window Toolkit (AWT). Subclasses of java.awt.Toolkit are used to bind various components to particular native resources.

You may have noticed that many of the methods in java.awt.Toolkit, java.awt.GraphicsEnvironment and other classes require the availability of display, keyboard and mouse. You may easily find such methods as they throw java.awt.HeadlessException in headless mode.

What is that headless mode?

Headless mode

Headless mode is a system configuration in which graphic adapter, keyboard or mouse are lacking. For example, mainframes or dedicated servers do not support a display, keyboard or mouse. On the other hand, such environments possess a substantial computing power, which can be used for the non-visual features realization.

All the AWT components with the exception of Canvas and Panel can not be operated in Headless mode. These include: applets, buttons, checkboxes, choices, dialogs, file dialogs, frames, labels, lists, menus, menubars, popup menus, page and print dialogs, scrollbars, scrollpanes, text components, windows, and their descendants. Such heavyweight components require a native "peer" at the operating system level, which cannot be guaranteed on "headless" machines.

Here is a brief list of what is available in Headless mode:

    * lightweight components: canvas, panels, Swing components (with the exception of JApplet, JDialog, JFrame and JWindow)
    * fonts and font metrics
    * colors
    * images
    * printing using java.awt.PrintJob, java.awt.print.* and javax.print.*
    * audio beep

Headless mode setup

To set up the Headless mode you should set the appropriate system property using System.setProperty() method. Property name is "java.awt.headless" and possible values are "true" or "false". Note, that this property shoult be set before the toolkit is initialized with Toolkit.getDefaultToolkit() method.

To check up the availability of the Headless mode you should use GraphicsEnvironment.isHeadless() method. This method checks the "java.awt.headless" property: if it equals to "true", it is assumed that java.awt.HeadlessException will be thrown from all the methods that are dependent on a display, keyboard, or mouse. This method *does not check* hardware configuration of the system, so it is possible to run the application in the Headless mode on a usual desktop.

Example

All the capabilities described above are represented in the integrated HeadlessBasics example. There are some comments in the source code to better understang Headless mode basics.

Compile the source code using javac compiler. Download grapefruit.jpg image and put it to the directory where the HeadlessBasics class is located and then run the example with `java HeadlessBasics`. You can run it without any display, keyboard or mouse in the system, for example, using a remote console.

Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • The link to HeadlessBasics.java does not work.

    Posted by: jglick on April 04, 2006 at 10:51 AM

  • Thanks, I will corect both links as soon as I can

    Posted by: ixmal on April 04, 2006 at 10:54 AM

  • It's good to learn about Headless Toolkit, as u r saying when we r using or implemnting certain feautres of Headless toolkit in our application and there is absence of Keyboard and Mouse in certain cases, thn how that application is going to be executed or response to a certain event.

    By using Headless Toolkit we only display certain content on our application.If it is the case then it is just a page which is providing certain information bt not doing any action...

    Posted by: gianeshwar123 on April 05, 2006 at 08:37 AM

  • Links have been repaired

    Posted by: ixmal on April 05, 2006 at 09:55 AM

  • You need to be aware of the Headless toolkit mostly if your application may do anything with AWT.

    Headless originally cropped up because folks wanted to do things like create charts and what not and render them in to JPGs and GIFs for web applications. But in earlier versions of Java, you were required to have some kind of display functionality on the machine that was creating the graph. So, for systems like Unix servers, this was typically done through firing up "fake" versions of X windows.

    But that was a configuration pain, so Sun (among others) created a version of the system that did not rely on the hosts implicit display capabilities so we could more easily use the Java APIs to draw and print graphics, even you could never "see" them on those machines.

    Posted by: whartung on April 05, 2006 at 03:08 PM





Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds