I have a dual monitor config and I want to run my GUI in a specific monitor if it is found. I tried to create my JFrame
window passing a GraphicConfiguration
object of my screen device, but it doesn't work, frame still display on the main screen.
How can I set the screen where my frame must be displayed?
Hold down the windows key and use the cursors to move the window around where you want it. If it's on the left monitor and you want it on the right, hold down windows key + press right arrow key and it'll shift across the screen.
To get the best possible match between your monitors, calibrate them to the same color temperature, brightness and gamma settings if possible. Use the monitor with the lowest brightness as your common denominator for all the other monitors.
A JFrame is like a Window with border, title, and buttons. 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.
public static void showOnScreen( int screen, JFrame frame ) { GraphicsEnvironment ge = GraphicsEnvironment .getLocalGraphicsEnvironment(); GraphicsDevice[] gs = ge.getScreenDevices(); if( screen > -1 && screen < gs.length ) { gs[screen].setFullScreenWindow( frame ); } else if( gs.length > 0 ) { gs[0].setFullScreenWindow( frame ); } else { throw new RuntimeException( "No Screens Found" ); } }
I have modified @Joseph-gordon's answer to allow for a way to achieve this without forcing full-screen:
public static void showOnScreen( int screen, JFrame frame ) { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice[] gd = ge.getScreenDevices(); if( screen > -1 && screen < gd.length ) { frame.setLocation(gd[screen].getDefaultConfiguration().getBounds().x, frame.getY()); } else if( gd.length > 0 ) { frame.setLocation(gd[0].getDefaultConfiguration().getBounds().x, frame.getY()); } else { throw new RuntimeException( "No Screens Found" ); } }
In this code I am assuming getDefaultConfiguration()
will never return null. If that is not the case then someone please correct me. But, this code works to move your JFrame
to the desired screen.
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