Reputation: 1706
I've noticed that if the screen mode is changed outside of Java that java.awt.GraphicsEnvironment.getMaximumWindowBounds()
still returns the size before the screen size changed.
But java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode()
works and shows the new resolution.
I've traced getMaximumBounds()
to a call to GraphicsDevice.getDefaultConfiguration()
that I suspect is using a stale configuration.
My question is: is there some way to tell Java the screen configuration has changed or is this just a bug?
Thanks.
Here is a simple app that shows the bug:
import java.awt.*;
public class test {
public static void print() {
try {
Rectangle r = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
System.out.println("max window bounds=" + r);
DisplayMode mode = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode();
System.out.println("mode=" + mode.getWidth() + "x" + mode.getHeight());
GraphicsConfiguration gc = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
System.out.println("insets=" + insets);
} catch (Exception e) {}
}
public static void main(String args[]) {
try {
print();
System.out.println("Change video mode and press Enter to continue...");
System.in.read();
print();
} catch (Exception e) {}
}
}
Upvotes: 1
Views: 1193
Reputation: 565
Based on Peter Quiring's answer, I added support for multiple monitors, based on where the component is located.
public static Rectangle getMaximumBounds(Component comp) {
GraphicsConfiguration gc = comp.getGraphicsConfiguration();
Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
DisplayMode mode = gc.getDevice().getDisplayMode();
int xPos = 0;
int yPos = 0;
for(GraphicsDevice device : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
Rectangle deviceBounds = device.getDefaultConfiguration().getBounds();
if(deviceBounds.contains(gc.getBounds())) {
xPos = gc.getBounds().x;
yPos = gc.getBounds().y;
}
}
Rectangle bounds = new Rectangle();
bounds.x = insets.left + xPos;
bounds.y = insets.top + yPos;
bounds.width = mode.getWidth() - (insets.left + insets.right);
bounds.height = mode.getHeight() - (insets.top + insets.bottom);
return bounds;
}
Upvotes: 0
Reputation: 1706
Here is a workaround that works for me:
public static Rectangle getMaximumBounds() {
Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration());
DisplayMode mode = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode();
Rectangle bounds = new Rectangle();
bounds.x = insets.left;
bounds.y = insets.top;
bounds.width = mode.getWidth() - (insets.left + insets.right);
bounds.height = mode.getHeight() - (insets.top + insets.bottom);
return bounds;
}
But I've noticed other awt function don't adjust due to the screen mode change. JPopupMenu.show() places menus way off. Pffff....oh well...
Upvotes: 1