Fortega's answer worked, however, some part is not needed (or is no longer needed with Java 8):
Rectangle does not need to be saved.- The code does not take into account dual screen configuration. In particular,
GraphicsConfiguration will change if the window changes screen. - As far as I tested, the only mandatory override is
setExtendedState .
When calculating a dual-screen configuration, at least on Windows , the code below does not work properly:
Rectangle maxBounds = new Rectangle(screenInsets.left + screenSize.x, screenInsets.top + screenSize.y, screenSize.x + screenSize.width - screenInsets.right - screenInsets.left, screenSize.y + screenSize.height - screenInsets.bottom - screenInsets.top);
On the following dual screen, configure:
- Left screen 1920x1080 (not primary), position: -1920, 0
- Right screen 1920x1080 (main), position: 0, 0
The maxBounds field will contain negative x (-1920), but setMaximizedBounds somehow expects a coordinate in the screen space (where (x,y) starts with (0,0) ), not the virtual screen:
- It will be
setMaximizedBounds(x=-1920,y=0,width=1920,height=1050) - Windows will see the window on the left screen (because I have one taskbar per screen, showing only the window on this screen), however, the window will not be displayed on the screen because it is outside.
- If the screen resolution or, even worse, its scale factor (on a Windows 10 laptop will use a scale factor, for example: 25%, which makes the screen βnot veryβ 1920x1080), then the above code does not adapt. For example, if there are 3 screens in my configuration, and the right-most of them is the main one, the window will not display well on the left and middle screens. I do not think I fixed this in the code below.
The following code works on dual-screen Windows:
@Override public synchronized void setExtendedState(final int state) { if ((state & Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH) { final GraphicsConfiguration cfg = getGraphicsConfiguration(); final Insets screenInsets = getToolkit().getScreenInsets(cfg); final Rectangle screenBounds = cfg.getBounds(); final int x = screenInsets.left + screenBounds.x * 0; final int y = screenInsets.top + screenBounds.y * 0; final int w = screenBounds.width - screenInsets.right - screenInsets.left; final int h = screenBounds.height - screenInsets.bottom - screenInsets.top; final Rectangle maximizedBounds = new Rectangle(x, y, w, h); System.out.println("cfg (" + cfg + ") screen.{bounds: " + screenBounds + ", insets: " + screenInsets + ", maxBounds: " + maximizedBounds); super.setMaximizedBounds(maximizedBounds); } super.setExtendedState(state); }
On a simple JFrame :
- When maximized, the left screen ("screen = 0") will have
cfg (D3DGraphicsConfig[dev=D3DGraphicsDevice[screen=0],pixfmt=0]) screen.{bounds: java.awt.Rectangle[x=-1920,y=0,width=1920,height=1080], insets: java.awt.Insets[top=0,left=0,bottom=30,right=0], maxBounds: java.awt.Rectangle[x=0,y=0,width=1920,height=1050] - When maximized, the right screen ("screen = 1") will have
cfg (D3DGraphicsConfig[dev=D3DGraphicsDevice[screen=1],pixfmt=0]) screen.{bounds: java.awt.Rectangle[x=0,y=0,width=1920,height=1080], insets: java.awt.Insets[top=0,left=0,bottom=30,right=0], maxBounds: java.awt.Rectangle[x=0,y=0,width=1920,height=1050]
source share