Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show JFrame in a specific screen in dual monitor configuration

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?

like image 470
blow Avatar asked Jan 07 '11 16:01

blow


People also ask

How do I make a window appear on a certain monitor?

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.

How do I match display settings on two monitors?

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.

How do I display a JFrame?

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.


2 Answers

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" );     } } 
like image 89
Joseph Gordon Avatar answered Sep 21 '22 08:09

Joseph Gordon


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.

like image 21
ryvantage Avatar answered Sep 21 '22 08:09

ryvantage