Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java GUI Fullscreen for Multiple Screens

I hope I'm not posting a duplicate question, but I wasn't able to find a question like this so maybe I'm safe? Anyway...

For the applications that I'm making, I'm going to have two applications (two separate processes and windows) open at the same time. The computer on which these applications will be running on will have multiple monitors. I want the first application/ window to fullscreen and occupy one of my monitors (easy part), and the other one to be fullscreen on the second monitor. If possible, I would like for them to initialize this way.

At the moment, I am making my windows fullscreen by using this code:

this.setVisible(false);
this.setUndecorated(true);
this.setResizable(false);
myDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
myDevice.setFullScreenWindow(this);

The class that this is in is an extension of the JFrame class and myDevice is of the type "GraphicsDevice". It's certainly possible that there's a better way to make my window fullscreen so that I can have two different applications fullscreen over two different monitors.

If I was unclear in any way, please say and I'll try to edit in clarifications!

like image 451
Sephallia Avatar asked Jun 01 '12 18:06

Sephallia


1 Answers

First, you need to position your frames on each screen devices.

frame1.setLocation(pointOnFirstScreen);
frame2.setLocation(pointOnSecondScreen);

Then to maximize a frame, simply call this on your JFrame:

frame.setExtendedState(Frame.MAXIMIZED_BOTH);

Here is a working example illustrating that:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class Test {
    protected void initUI() {
        Point p1 = null;
        Point p2 = null;
        for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
            if (p1 == null) {
                p1 = gd.getDefaultConfiguration().getBounds().getLocation();
            } else if (p2 == null) {
                p2 = gd.getDefaultConfiguration().getBounds().getLocation();
            }
        }
        if (p2 == null) {
            p2 = p1;
        }
        createFrameAtLocation(p1);
        createFrameAtLocation(p2);
    }

    private void createFrameAtLocation(Point p) {
        final JFrame frame = new JFrame();
        frame.setTitle("Test frame on two screens");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel(new BorderLayout());
        final JTextArea textareaA = new JTextArea(24, 80);
        textareaA.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY, 1));
        panel.add(textareaA, BorderLayout.CENTER);
        frame.setLocation(p);
        frame.add(panel);
        frame.pack();
        frame.setExtendedState(Frame.MAXIMIZED_BOTH);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test().initUI();
            }
        });
    }

}
like image 199
Guillaume Polet Avatar answered Oct 17 '22 12:10

Guillaume Polet