Java swing component location on screen off by 10 pixels on x-axis in Windows 10

Viewed 293

I've got a JFrame that I'd like to position at the upper left corner of my display, (0,0). I set the frame location using setLocation() before setting it visible. This results in the correct Y position, but the X position is off by 10 pixels, i.e the frame's upper left corner is 10 pixels to the right of the edge of the screen. When I move the window closer to the left edge, getLocationOnScreen() starts reporting negative numbers as if the frame was off screen, which it is not. When moving the window all the way to the right side of the screen getLocationOnScreen() reports numbers that imply 10 pixels are off screen to the right. It's as if java/swing's view of my display is offset by 10 pixels to the right.

I've tested this under Java 8, 11, and 14 and with display scaling on and off with the same results. Under macOS, the window is placed at the correct location.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

public class LocationTest {

    public static void main(String[] args) {
        JFrame frame = new JFrame("Location Test");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setSize(200, 200);
        frame.setLocation(new Point(0, 0));
        frame.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentMoved(ComponentEvent e) {
                super.componentMoved(e);
                System.out.println(frame.getLocationOnScreen());
            }
        });
        frame.setVisible(true);
        System.out.println("frame.getLocationOnScreen() = " + frame.getLocationOnScreen());
    }
}
0 Answers
Related