Show Jframe in a Specific Screen in Dual Monitor Configuration

Show JFrame in a specific screen in dual monitor configuration

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" );
}
}

Keep the jframe open on a dual monitor configuration in Java

As long as I use setFullScreenWindow I do get the same issue. Replacing the frames by dialogs solves the issue but I guess you really want to have a frame, not a dialog.

So the solution is to manually maximize the frame instead of using setFullScreenWindow:

import java.awt.*;
import javax.swing.*;

public class MultiMonitorFrame extends JFrame {

public static void showFrameOnScreen(Window frame, int screen) {
GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] graphicsDevices = graphicsEnvironment.getScreenDevices();
GraphicsDevice graphicsDevice = ( screen > -1 && screen < graphicsDevices.length ) ? graphicsDevices[screen] : graphicsDevices.length > 0 ? graphicsDevices[0] : null;
if (graphicsDevice == null)
{
throw new RuntimeException( "There are no screens !" );
}
Rectangle bounds = graphicsDevice.getDefaultConfiguration().getBounds();
frame.setSize(bounds.width, bounds.height);
frame.setLocation(bounds.x, bounds.y);
}

public static void main(String[] args) {
JFrame frame1 = new JFrame("First frame");
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setVisible(true);
frame1.setAlwaysOnTop(true);
JFrame frame2 = new JFrame("Second frame");
frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame2.setVisible(true);
frame2.setAlwaysOnTop(true);
showFrameOnScreen(frame1, 1);
showFrameOnScreen(frame2, 2);
}
}

This shows the two frames each on one monitor and the frames do not get minimized when using ALT-Tab or clicking on the desktop.

Output jFrame to second monitor if possible

Try the following:

public static void main( String[] args )
{
java.awt.GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();

for ( int i = 0; i < devices.length; i++ )
{
System.out.println( "Device " + i + " width: " + devices[ i ].getDisplayMode().getWidth() );
System.out.println( "Device " + i + " height: " + devices[ i ].getDisplayMode().getHeight() );
}
}


Related Topics



Leave a reply



Submit