1

I am writing a program designed to work on a two monitor system. I have to separate JFrame objects, and have it so be default, the first frame instance opens. The user then has to drag that frame over to a specific monitor, or leave it in place. When they click a button on that frame, I want the program to open up the second frame on the opposite monitor.

So, How would I figure out which monitor a frame object is on, and then tell another frame object to open on the opposite one?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    1) What is the app. specifically? See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) 2) Look into the [`GraphicsEnvironment`](http://docs.oracle.com/javase/7/docs/api/java/awt/GraphicsEnvironment.html) & [`GraphicsDevice`](http://docs.oracle.com/javase/7/docs/api/java/awt/GraphicsDevice.html) objects it reveals. Corrected as per @mKorbel tip. +1 – Andrew Thompson Jan 15 '13 at 13:13

1 Answers1

3

Looking up GraphicsEnvironment, you can easily find out the bounds and location of each screen. After that, it is just a matter of playing with the location of the frames.

See small demo example code here:

import java.awt.Frame;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class TestMultipleScreens {

    private int count = 1;

    protected void initUI() {
        Point p = null;
        for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
            p = gd.getDefaultConfiguration().getBounds().getLocation();
            break;
        }
        createFrameAtLocation(p);
    }

    private void createFrameAtLocation(Point p) {
        final JFrame frame = new JFrame();
        frame.setTitle("Frame-" + count++);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        final JButton button = new JButton("Click me to open new frame on another screen (if you have two screens!)");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                GraphicsDevice device = button.getGraphicsConfiguration().getDevice();
                Point p = null;
                for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
                    if (!device.equals(gd)) {
                        p = gd.getDefaultConfiguration().getBounds().getLocation();
                        break;
                    }
                }
                createFrameAtLocation(p);
            }
        });
        frame.add(button);
        frame.setLocation(p);
        frame.pack(); // Sets the size of the unmaximized window
        frame.setExtendedState(Frame.MAXIMIZED_BOTH); // switch to maximized window
        frame.setVisible(true);
    }

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

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

}

Yet, consider reading carefully The Use of Multiple JFrames, Good/Bad Practice? because they bring very interesting considerations.

Community
  • 1
  • 1
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • Thanks. For the application I am writing, I NEED to have multiple frames, as it is designed for 'Presentation' so I can't really do anything else. – Austin Witherspoon Jan 15 '13 at 13:42