1

how to get width and height of screen of current monitor in multimonitor scenarios in java.

GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
    int width = gd.getDisplayMode().getWidth();
    int height = gd.getDisplayMode().getHeight();

This piece of code gives the width and height of same monitor even if I run in other monitor.

How do you get the width and height of current monitor. Please help.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
sridhar
  • 1,117
  • 5
  • 31
  • 59
  • Here is your answer: http://stackoverflow.com/questions/3680221/how-can-i-get-the-monitor-size-in-java – MuGiK Dec 11 '14 at 09:33
  • Hi MuGiK, thanks.. I need the resolution of current monitor in use. It gives me default device and not current device I believe. Please help me identifying the size of current monitor in use and not default monitor. GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice(); – sridhar Dec 11 '14 at 09:35

4 Answers4

1

Try:

GraphicsDevice gd[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();

Then you'll have all your monitor in the array.

Now get width and height:

int width = gd[n].getDisplayMode().getWidth();
int height = gd[n].getDisplayMode().getHeight();
pherris
  • 17,195
  • 8
  • 42
  • 58
MuGiK
  • 351
  • 4
  • 13
1

It depends on what you mean by the current screen. If it is the screen where the mouse cursor is, you can get the device with:

GraphicsDevice gd = MouseInfo.getPointerInfo().getDevice();

On the other hand, if you need the device where a window is located, use:

GraphicsDevice gd = frame.getGraphicsConfiguration().getDevice();

From these you can obtain the dimensions:

int width = gd.getDisplayMode().getWidth();
int height = gd.getDisplayMode().getHeight();
kiheru
  • 6,588
  • 25
  • 31
0

In addition to the comment, you may obtain all available monitors:

GraphicsDevice.getLocalGraphicsEnvironment().getScreenDevices()

and know them resolutions

cybersoft
  • 1,453
  • 13
  • 31
0

You can get screen dimensions using Toolkit as follows:

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double width = screenSize.getWidth();
double height = screenSize.getHeight();

For multi-screen try following:

GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int width = gd.getDisplayMode().getWidth();
int height = gd.getDisplayMode().getHeight();
Darshan Lila
  • 5,772
  • 2
  • 24
  • 34