Question

In my application, I have 2 windows (jframe), one for controlling and one for displaying things (like powerpoint presentation mode).

How can I specify one window is opened in screen no. 1 and the other is opened in screen no. 2 when I start the application?

The method below somehow works, but the thing is the window on the second screen always maximized and I don't want it to be maximized. But it seems the only way to connect GraphicsDevice and JFrame is the function called setFullScreenWindow.

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" );
    }
}
Was it helpful?

Solution

You can define a JFrame with a GraphicsDevice.

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

for (int j = 0; j < gs.length; j++) { 
    JFrame f = new JFrame(gs[j].getDefaultConfiguration());
    // Rest of the code
}

OTHER TIPS

If the screens form one large virtual screen, one can have GraphicsConfigurations with following bounds:

  • Rectangle[x=-1280,y=74,width=1280,height=1024]
  • Rectangle[x=0,y=0,width=1920,height=1080]
  • Rectangle[x=1920,y=0,width=1920,height=1080]

These were 3 monitors side-by-side. So:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gds = ge.getScreenDevices();
for (GraphicsDevice gd : gds) {
    int x = gd.getDefaultConfiguration().getBounds().x;
    int y = gd.getDefaultConfiguration().getBounds().y;
    JFrame frame = new NewJFrame();
    frame.setLocation(x, y);
    frame.setVisible(true);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top