7

I am aware from this question that you can get the current displays as a GraphicsDevice[] in a swing application with the following code:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gd = ge.getScreenDevices();

I understand that from here I could set the device myself, let the user select which to use in a menu, etc. My goal is to detect which of these GraphicsDevices the application is currently displayed in, so I can default to fullscreening in that current GraphicsDevice without bothering the user.

I am using code like the following to fullscreen my application:

JFrame frame = new JFrame();

// ... when I want to fullscreen:
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
if (gd.isFullScreenSupported()){
    frame.dispose();
    frame.setUndecorated(true);
    frame.setAlwaysOnTop(true);
    gd.setFullScreenWindow(frame);
    frame.setVisible(true);
}

I cannot find anything in the GraphicsDevice or GraphicsEnvironment APIs to get the current GraphicsDevice the JFrame is displayed on. I am aware there may be possible hacks by positioning the window directly (as described in the link above), but I am wondering if there is an easier/more elegant solution.

Community
  • 1
  • 1
aaroncarsonart
  • 1,054
  • 11
  • 27
  • 3
    Generally I simply use the `GraphicsConfiguration`s of the `GraphicsDevice`s to test if the Window is within the bounds of the device, for [example](http://stackoverflow.com/questions/12158548/how-to-figure-out-on-which-screen-a-jdialog-is-shown/12170555#12170555) – MadProgrammer Jul 30 '15 at 07:36
  • Awesome, so I can measure which device the window overlaps with the greatest area? – aaroncarsonart Jul 30 '15 at 07:39
  • Yeah, the example (`getGraphicsDevice(Component)`) does that too ;) – MadProgrammer Jul 30 '15 at 07:41
  • Check out Santhosh's answer. Works perfectly, just like the code example you wrote (which works too), but why reinvent the wheel when it is already in the API? – aaroncarsonart Jul 30 '15 at 08:19
  • the likely reason is because it either didn't do what I wanted or didn't return the results I wanted or expected – MadProgrammer Jul 30 '15 at 08:44

1 Answers1

10
GraphicsDevice device = jframe.getGraphicsConfiguration().getDevice()
Santhosh Kumar Tekuri
  • 3,012
  • 22
  • 22