Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a workaround for Windows 10 invisible frame border in java?

I've discovered an issue with Windows 10 and Java.

JFrame frame = new JFrame();
frame.setVisible(true);

When I create a new JFrame and show it on the screen, it normally should have the screen coordinates x=0 and y=0. It actually does have those coordinates on windows 10.

But, what is really ugly, is, that it seems all windows in Windows 10 have some kind of "invisible" borders around them. So in this case it looks like the position of the Window is x=7 and y=0:

Invisible window border causes an optically space to neighbor elements

This is only a simple case where it just looks so ugly. My software has some logic which will "snap" one window onto another when you drag it in the near of the other one. I think you can imagine how ugly it looks if theres a (2*7px=) 14px space between the two windows altough it should be shown for the user as "linked" or "snapped" together.

Expecatation of "snapped" windows and reality

On Windows 7 this has worked perfectly!

So does somebody know this problem and have any idea, how a workaround could be possible? I think of maybe finding out the current name of the windows theme and operating system name. And if it is "Aero" and "Windows 10", always just calculate x positions with -7px. Would that be somehow possible? Or maybe directly finding out what is the width of the invisible area?

Thanks for any idea about this topic.

like image 748
mstrd308 Avatar asked Jan 08 '19 16:01

mstrd308


People also ask

How do you make a frame visible in Java?

To make the frame visible, invoke setVisible(true) on it.

How do I add a border to a JFrame in Java?

Yes you can draw the borders around the undecorated JFrame. Just simply get the root pane of the JFrame and set its borders by setBorder(Border border) method. As far as I know, this will only shrink down the root pane so it can draw the border inside the frame itself.


1 Answers

This could solve your problem :

public static void setJFrameBounds(JFrame frame, int x, int y, int width, int height) {
    Insets insets = ((Window)frame).getInsets();
    frame.setBounds(x - insets.left, y, width, height);
}  

Edit : the JFrame must be visible before calling the function

like image 200
Doomny 58 Avatar answered Nov 14 '22 23:11

Doomny 58