I have two monitors
I write very small Swing Java code to collect info of all screen devices combine changing display mode with one or two display screen by setting Display
It may be tricky. But from a quick look at the source code, you might try some reflection.
Disclaimer: Many things can go wrong when using reflection. You should be aware of the fact that you are relying on unspecified behavior here. If the underlying implementation changes, then the following example program might no longer work...
...although I consider this as "unlikely", at least
The following is an example showing how this might work:
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class GraphicsEnvironmentTest
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("Demo get info screen devices");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Print info screen devices");
button.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
printInfoAllScreenDevices();
}
});
frame.add(button);
frame.setSize(500, 300);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static void printInfoAllScreenDevices()
{
GraphicsDevice graphicsDevices[] = getGraphicsDevices();
System.out.println("Found "+graphicsDevices.length+" devices:");
for (int i=0; i<graphicsDevices.length; i++)
{
System.out.println(graphicsDevices[i]);
}
}
/**
* Queries the local graphics environment for the available graphics
* devices. This uses reflection internally. If anything goes wrong
* with the reflective call, a RuntimeException will be thrown.
*
* @return The available graphics devices.
* @throws RuntimeException If the reflective calls fail
*/
private static GraphicsDevice[] getGraphicsDevices()
{
GraphicsEnvironment graphicsEnvironment =
GraphicsEnvironment.getLocalGraphicsEnvironment();
Class<?> c = graphicsEnvironment.getClass();
Method getNumScreensMethod = null;
boolean getNumScreensMethodWasAccessible = false;
Method makeScreenDeviceMethod = null;
boolean makeScreenDeviceMethodWasAccessible = false;
try
{
getNumScreensMethod =
c.getDeclaredMethod("getNumScreens");
getNumScreensMethodWasAccessible =
getNumScreensMethod.isAccessible();
getNumScreensMethod.setAccessible(true);
makeScreenDeviceMethod =
c.getDeclaredMethod("makeScreenDevice", int.class);
makeScreenDeviceMethodWasAccessible =
makeScreenDeviceMethod.isAccessible();
makeScreenDeviceMethod.setAccessible(true);
int numScreens =
(Integer) getNumScreensMethod.invoke(graphicsEnvironment);
GraphicsDevice graphicsDevices[] = new GraphicsDevice[numScreens];
for (int i = 0; i < numScreens; i++)
{
Object object =
makeScreenDeviceMethod.invoke(graphicsEnvironment, i);
graphicsDevices[i] = (GraphicsDevice) object;
}
return graphicsDevices;
}
catch (NoSuchMethodException e)
{
throw new RuntimeException(e);
}
catch (SecurityException e)
{
throw new RuntimeException(e);
}
catch (IllegalAccessException e)
{
throw new RuntimeException(e);
}
catch (IllegalArgumentException e)
{
throw new RuntimeException(e);
}
catch (InvocationTargetException e)
{
throw new RuntimeException(e);
}
finally
{
if (getNumScreensMethod != null)
{
getNumScreensMethod.setAccessible(
getNumScreensMethodWasAccessible);
}
if (makeScreenDeviceMethod != null)
{
makeScreenDeviceMethod.setAccessible(
makeScreenDeviceMethodWasAccessible);
}
}
}
}