43

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

skaffman
  • 398,947
  • 96
  • 818
  • 769
Andrew
  • 703
  • 2
  • 8
  • 12

12 Answers12

78
java.awt.Toolkit.getDefaultToolkit().getScreenSize()
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
Ben McCann
  • 18,548
  • 25
  • 83
  • 101
18

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;
    }
Lawrence Dol
  • 63,018
  • 25
  • 139
  • 189
  • +1 for showing how to grab the insets. They can be important, especially on OS X if you want to have a full size window without overlapping the dock. – Samuel Sjöberg Dec 21 '09 at 15:26
  • @SoftwareMonkey is there anyway to get the graphicsConfiguration without having to create a new Frame ? – Pacerier Nov 10 '11 at 02:19
  • -1: there is no `Toolkit.getDefaultToolkit().getGraphicsConfiguration()` – jan Mar 20 '14 at 11:06
  • @Jan: Not sure what happened here, but the original code came directly from compilable, working code I had in use in real apps. Anyway, it is now updated to use GraphicsEnvironment to get the default (primary) display bounds and insets, if the supplied window is null - this eliminates the need for a dummy `Frame`. And, again, this code is working and in production for Java 4+ (and still currently works in Java 7). – Lawrence Dol Mar 20 '14 at 18:56
  • @Jan: The only thing I can think is that I made a copy/paste error (my original code *was* wrapped in some try/catch handling in case it was run on an earlier JVM that didn't have the APIs, something which used to happen quite often way back). – Lawrence Dol Mar 20 '14 at 19:18
  • I added a solution [below](http://stackoverflow.com/a/38191173/3142960), which uses this (correct) method, but avoids getting the graphics configuration twice. – BlakeTNC Jul 04 '16 at 19:31
17

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;
}
Pacerier
  • 86,231
  • 106
  • 366
  • 634
5

The following code should do it (haven't tried it):

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
gd.getDefaultConfiguration().getBounds().getWidth();

edit:

For multiple monitors you should use the following code (taken from the javadoc of java.awt.GraphicsConfiguration:

  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());
      }
  } 
tangens
  • 39,095
  • 19
  • 120
  • 139
5

Toolkit has a number of classes that would help:

  1. getScreenSize - raw screen size
  2. getScreenInsets - gets size of toolbar, dock
  3. getScreenResolution - dpi

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.

Michael Brewer-Davis
  • 14,018
  • 5
  • 37
  • 49
2

The OP probably wanted something like this:

Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
MTCoster
  • 5,868
  • 3
  • 28
  • 49
jmoreno
  • 12,752
  • 4
  • 60
  • 91
1
Toolkit.getDefaultToolkit().getScreenSize().getWidth()
Alex Ntousias
  • 8,962
  • 8
  • 39
  • 47
0

You can get it by using the AWT Toolkit.

unholysampler
  • 17,141
  • 7
  • 47
  • 64
0

Toolkit.getScreenSize().

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dan Dyer
  • 53,737
  • 19
  • 129
  • 165
0

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.

Community
  • 1
  • 1
jan
  • 2,741
  • 4
  • 35
  • 56
0

A good way of detecting whether or not something is within visual bounds, is using

Screen.getScreensForRectangle(x, y, width, height).isEmpty();
tvkanters
  • 3,519
  • 4
  • 29
  • 45
0

This is an improvement to the multi-monitor solution posted (above) by Lawrence Dol. As in his solution, this code accounts for multiple monitors and task-bar insets. The included functions are: getScreenInsets(), getScreenWorkingArea(), and getScreenTotalArea().

Changes from the Lawrence Dol version:

  • This avoids getting the graphics configuration twice.
  • Added a function for getting the total screen area.
  • Renamed the variables for clarity.
  • Added Javadocs.

Code:

/**
 * getScreenInsets, This returns the insets of the screen, which are defined by any task bars
 * that have been set up by the user. This function accounts for multi-monitor setups. If a
 * window is supplied, then the the monitor that contains the window will be used. If a window
 * is not supplied, then the primary monitor will be used.
 */
static public Insets getScreenInsets(Window windowOrNull) {
    Insets insets;
    if (windowOrNull == null) {
        insets = Toolkit.getDefaultToolkit().getScreenInsets(GraphicsEnvironment
                .getLocalGraphicsEnvironment().getDefaultScreenDevice()
                .getDefaultConfiguration());
    } else {
        insets = windowOrNull.getToolkit().getScreenInsets(
                windowOrNull.getGraphicsConfiguration());
    }
    return insets;
}

/**
 * getScreenWorkingArea, This returns the working area of the screen. (The working area excludes
 * any task bars.) This function accounts for multi-monitor setups. If a window is supplied,
 * then the the monitor that contains the window will be used. If a window is not supplied, then
 * the primary monitor will be used.
 */
static public Rectangle getScreenWorkingArea(Window windowOrNull) {
    Insets insets;
    Rectangle bounds;
    if (windowOrNull == null) {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        insets = Toolkit.getDefaultToolkit().getScreenInsets(ge.getDefaultScreenDevice()
                .getDefaultConfiguration());
        bounds = ge.getDefaultScreenDevice().getDefaultConfiguration().getBounds();
    } else {
        GraphicsConfiguration gc = windowOrNull.getGraphicsConfiguration();
        insets = windowOrNull.getToolkit().getScreenInsets(gc);
        bounds = gc.getBounds();
    }
    bounds.x += insets.left;
    bounds.y += insets.top;
    bounds.width -= (insets.left + insets.right);
    bounds.height -= (insets.top + insets.bottom);
    return bounds;
}

/**
 * getScreenTotalArea, This returns the total area of the screen. (The total area includes any
 * task bars.) This function accounts for multi-monitor setups. If a window is supplied, then
 * the the monitor that contains the window will be used. If a window is not supplied, then the
 * primary monitor will be used.
 */
static public Rectangle getScreenTotalArea(Window windowOrNull) {
    Rectangle bounds;
    if (windowOrNull == null) {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        bounds = ge.getDefaultScreenDevice().getDefaultConfiguration().getBounds();
    } else {
        GraphicsConfiguration gc = windowOrNull.getGraphicsConfiguration();
        bounds = gc.getBounds();
    }
    return bounds;
}
BlakeTNC
  • 941
  • 11
  • 14