Does anyone know how you would get the screen width in java? I read something about some toolkit method but I\'m not quite sure what that is.
Thanks, Andrew
You can get it by using the AWT Toolkit.
Toolkit.getScreenSize().
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
The working area is the desktop area of the display, excluding taskbars, docked windows, and docked tool bars.
If what you want is the "working area" of the screen, use this:
public static int GetScreenWorkingWidth() {
return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().width;
}
public static int GetScreenWorkingHeight() {
return java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds().height;
}
Here are the two methods I use, which account for multiple monitors and task-bar insets. If you don't need the two methods separately, you can, of course, avoid getting the graphics config twice.
static public Rectangle getScreenBounds(Window wnd) {
Rectangle sb;
Insets si=getScreenInsets(wnd);
if(wnd==null) {
sb=GraphicsEnvironment
.getLocalGraphicsEnvironment()
.getDefaultScreenDevice()
.getDefaultConfiguration()
.getBounds();
}
else {
sb=wnd
.getGraphicsConfiguration()
.getBounds();
}
sb.x +=si.left;
sb.y +=si.top;
sb.width -=si.left+si.right;
sb.height-=si.top+si.bottom;
return sb;
}
static public Insets getScreenInsets(Window wnd) {
Insets si;
if(wnd==null) {
si=Toolkit.getDefaultToolkit().getScreenInsets(GraphicsEnvironment
.getLocalGraphicsEnvironment()
.getDefaultScreenDevice()
.getDefaultConfiguration());
}
else {
si=wnd.getToolkit().getScreenInsets(wnd.getGraphicsConfiguration());
}
return si;
}
If you need the resolution of the screen that a certain component is currently assigned to (something like most part of the root window is visible on that screen), you can use this answer.
Toolkit has a number of classes that would help:
We end up using 1 and 2, to compute usable maximum window size. To get the relevant GraphicsConfiguration, we use
GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()[0].getDefaultConfiguration();
but there may be smarter multiple-monitor solutions.