Java full-screen GUI for multiple screens

I hope I won’t post a recurring question, but I couldn’t find such a question, maybe I'm safe? Anyway...

For the applications that I create, I am going to open two applications at the same time (two separate processes and windows). The computer on which these applications will run will have multiple monitors. I want the first application / window to be displayed in full screen mode and occupy one of my monitors (the light part), and the second - full screen mode on the second monitor. If possible, I would like them to initialize this path.

I am currently making windows in full screen using this code:

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

The class in which it is located is an extension of the JFrame class, and myDevice is of type "GraphicsDevice". Of course, it’s possible that there is a better way to make my window full screen so that I have two different applications in full screen on two different monitors.

If I were unclear in any way, tell me, and I will try to amend the clarifications!

+5
source share
1 answer

First, you need to place your frames on each screen device.

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

Then, to maximize the frame, just name it 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();
            }
        });
    }

}
+5
source

All Articles