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.
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.
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.
JFrame is a class of the javax. swing package that is extended by 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.
GraphicsDevice device = jframe.getGraphicsConfiguration().getDevice()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With