Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Swing - Detect which display a JFrame is displayed on

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.

like image 296
aaroncarsonart Avatar asked Jul 30 '15 07:07

aaroncarsonart


People also ask

Which method will cause a JFrame to be displayed?

We can implement most of the java swing applications using JFrame. By default, a JFrame can be displayed at the top-left position of a screen. We can display the center position of JFrame using the setLocationRelativeTo() method of Window class.

Which method is used to show frame window on screen in java?

Calling setVisible(true) makes the frame appear onscreen. Sometimes you might see the show method used instead. The two usages are equivalent, but we use setVisible(true) for consistency's sake.

Is JFrame in Swing?

JFrame is a class of the javax. swing package that is extended by java.

Which method will cause a JFrame to get displayed in Advance java?

Solution. Following example demonstrates how to display message in a new frame by creating a frame using JFrame() & using JFrames getContentPanel(), setSize() & setVisible() methods to display this frame.


1 Answers

GraphicsDevice device = jframe.getGraphicsConfiguration().getDevice()
like image 186
Santhosh Kumar Tekuri Avatar answered Oct 06 '22 01:10

Santhosh Kumar Tekuri