Search code examples
javasizescreentoolkit

Java Toolkit Getting Second screen size


I have two screens plugged into my computer and was wondering if there was a way in JFrame or Toolkit of detecting which screen the window is on?

I have this code:

java.awt.Toolkit.getDefaultToolkit().getScreenSize();

Which gets my screen size of my main screen, but how can I get the size of my second screen, or detect which screen the window is on?


Solution

  • You should take a look at GraphicsEnvironment.

    In particular, getScreenDevices():

    Returns an array of all of the screen GraphicsDevice objects.

    You can get the dimensions from those GraphicDevice objects (indirectly, via getDisplayMode). (That page also shows how to put a frame on a specific device.)

    And you can get from a JFrame to its device via the getGraphicsConfigration() method, which returns a GraphicsConfiguration that has a getDevice(). (The getIDstring() method will probably enable you to differentiate between the screens.)