User-1204
User-1204

Reputation: 1

How to move JFrames on other screens?

I've created a programme, which opens 4 Jframes containing applets. I would like to move these Frames from my Main-Screen to my second, but it's not working.

I've tried with the .setLocation(x,y)-method, but it the jframes still won't move over the edge of the screen.

I even tried with getting the bound with the graphicsdevice.getdefaultconfiguration.getBounds(), but the result is "0", so it opens the JFrames on 0,0 on my main screen every time.

Hope, anyone understands my problem and can help me :D

P.S.: Sorry for my bad english :S

Upvotes: 0

Views: 56

Answers (1)

Mattias Isegran Bergander
Mattias Isegran Bergander

Reputation: 11909

Depending on your 2-monitor setup, left or right of the center screen, you can just use negative value for x to move it to the left monitor, or a value larger than the width of the center monitor to move it to the right monitor.

Beware: If you have different height of the monitors make sure the y-value is within the bounds of it too.

For example if you have a monitor to the left of the center screen:

JFrame f = new JFrame();
f.getContentPane().add(new JLabel("Hello world"));
f.pack();
f.setVisible(true);
f.setLocation(0, 200);

JFrame f2 = new JFrame();
f2.getContentPane().add(new JLabel("Hello world 2"));
f2.pack();
f2.setVisible(true);
f2.setLocation(-400, 500);

Monitors vertically stacked left as an exercisefor the dev. :)

Of course if you don't want to hard code those values there is a way to find the different screen sizes and offsets in x and y (if not the same size for example):

    GraphicsDevice[] gds = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
    for (GraphicsDevice gd : gds) {
        for(GraphicsConfiguration gc : gd.getConfigurations()) {
            Rectangle bounds = gc.getBounds();
            System.out.println("x,y: "+bounds.getX() + "," + bounds.getY() + " width,height: " + bounds.getWidth() + "x" + bounds.getHeight());
        }
    }

For me right now, having two screens, with one big screen 27" center and one 24" to the left:

x,y: 0.0,0.0 width,height: 2560.0x1440.0
x,y: -1920.0,232.0 width,height: 1920.0x1200.0

Explanation: The left screen's left side is at x=-1920

Upvotes: 1

Related Questions