To compute the display size, you can use (taken from GraphicsConfiguration javadoc)
Rectangle virtualBounds = new Rectangle();
GraphicsEnvironment ge = GraphicsEnvironment.
getLocalGraphicsEnvironment();
GraphicsDevice[] gs =
ge.getScreenDevices();
for (int j = 0; j < gs.length; j++) {
GraphicsDevice gd = gs[j];
GraphicsConfiguration[] gc =
gd.getConfigurations();
for (int i=0; i < gc.length; i++) {
virtualBounds =
virtualBounds.union(gc[i].getBounds());
}
}
You can then query the width
and height
of virtualBounds
.
This will also work in multi-monitor setups.
As far as I know, there is no JDK supported method to detect changes, so polling is your only bet, or use JNA and native code. If your top-level window is maximized, then resizing the display will also resize any maximized windows, so you could listen for changes there also. See this sun tutorial for examples of writing component listeners.
If you don't have a top-level maximized window, then you may also be able to achieve the same effect by creating a hidden maximized window. I can't say for sure if this will work, but it is worth trying.