0

Context


In Microsoft Windows, I am able make an application's window occupy half the of display by using the shortcuts:

  • ⊞ Win +
  • ⊞ Win +

These shortcuts cause a "split screen," as seen in this screenshot:

split screen

I would like to programmatically size and position a JFrame in the same manner. However, I do not know how to achieve the desired result.

I thought that solution was:

  1. Set the frame's width to the half the screen width.
  2. Set the frame's height to the screen height (minus the taskbar height).
  3. Set the frame's position to (0,0).

Here is an MCVE:

JFrame testFrame = new JFrame();

Rectangle windowBounds = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
testFrame.setSize(windowBounds.width / 2, windowBounds.height);
testFrame.setLocation((int) windowBounds.getMinX(), (int) windowBounds.getMinY());

testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
testFrame.setVisible(true);

This nearly works, but the window has small margins on the bottom and the left:

enter image description here

Question


On a Windows computer, is there a way to programmatically split the screen using Java, or perhaps JNI?

11
  • Print the values of windowBounds.getMinX() and windowBounds.getMinY(), as well as getMaximumWindowBounds(), and make sure they're reasonable. Sep 22 at 20:37
  • 4
    Why do you want to do it yourself? If the users of your program want to do it with your program they can do it without special support from your program - it is Windows that does it with the programs. Sep 22 at 21:48
  • You're talking about an operation which is controlled by the OS, why do you feel the need to replicate it in your code? Sep 22 at 22:21
  • You probably want testFrame.setExtendedWindowState(Frame.MAXIMIZED_VERT).
    – VGR
    Sep 23 at 5:50
  • @ThomasKläger The functionality was requested for an application within my company. I personally think this is solving a problem that doesn't exist but the company wants a button to split the screen (since keyboard shortcuts are allegedly too difficult for "non technically minded" people to learn). Sep 24 at 1:57

2 Answers 2

1

Here's a workaround that achieved [what I believe to be] your desired result.
(Oracle JDK 21 on Windows 10)

  • Initially set the JFrame to be maximized, i.e. take up the whole screen.
  • Add a WindowListener to the JFrame.
  • When the JFrame is displayed on the [computer] screen, adjust its size.
    (Method windowOpened is called only once – when the window is initially displayed.)
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Window;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

public class TstFrame extends WindowAdapter {

    @Override // java.awt.event.WindowAdapter
    public void windowOpened(WindowEvent event) {
        Window window = event.getWindow();
        Dimension dim = window.getSize();
        window.setSize(dim.width / 2, dim.height);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            JFrame testFrame = new JFrame();
            testFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
            testFrame.addWindowListener(new TstFrame());
            testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            testFrame.setVisible(true);
        });
    }
}

Screen capture:

screen capture

1
  • This comes close enough to the desired behavior. Thank for answering rather than lecturing! Just trying to do my job here :P Sep 25 at 11:56
0

You are in a highly volatile area. Depending on user preferences the values for margin width, taskbar height/width or other settings may vary. Unless you use the 'Window Manager' function to position the window you will run a lottery of having the right values. Doing that from Java may require to involve JNI and the correct Win32 API call.

Hence I'd try to avoid that situation. Why do you need it at all? And could it help if your application saved the window position and size upon exit, so it can remember them when starting up next time? Then the user can still choose where to have the window while he is not fixed on the left or right half of the screen.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.