views:

116

answers:

3

How do you turn on and off a monitor from within a Java application?

In case you're wondering why, this is a kiosk style application where it would be great to turn off the monitors at night. Yes you can do it within the screensaver settings of the machine, but it would be great to do it programatically and avoid having to configure it on each machine.

+6  A: 

Assuming you deploy your Java Applications on Windows, you can use this WIN32API functions:

// turn off monitor
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) 2); 

// turn on monitor
SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM) -1);    

Then you write a little C-JNI wrapper to functions that call the mentioned SendMessage and use the little wrapper to turn off the monitor from Java.

Pablo Santa Cruz
Alternatively to JNI, you could use JNA to call the WIN32API directly, avoiding the need for any C glue code. http://en.wikipedia.org/wiki/Java_Native_Access
Andy Thomas-Cramer
+2  A: 

While you can query the monitor configurations with Java, there is no way to turn the monitors on and off programatically without using JNI.

Erick Robertson
A: 

You can open a full screen swing window, make it completely black, so the monitor, as far as observable behavior is concerned, is turned off.

irreputable
@irreputable: Good idea! To make your solution even better, one could attach a webcam to the kiosk-PC and take a photo before "shutting down". Then it would be possible to calculate (maybe with the Java Advanced Imaging (JAI) API) and show a picture of the reflections, that would be shown if the monitor was really turned off. Better than showing just a black screen, now over 95 percent of the people walking by couldn't tell the difference. Oh, and if the monitor has a power-on-LED on the front, the use of a touch-up pencil in the color of the case to paint it over would be most appropriate.
bobndrew