Search code examples
javamultiple-monitors

Get location of secondary monitor. (right of /left of Primary)


I am working on a programm which needs to find objects on a screen, works fine so far, but I got a problem with multi-monitor configurations.

The GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()-class is able to give almost all details I need to define my search-area over multiple Screens but not the order of the devices. (e.g. secondary monitor is right of primary).

Therefore I am looking for a way to find out the screen-locations of primary and secondary monitors in, Java.


Solution

  • You can try reading GraphicsDevice.html#getIDstring() value to determine ID of the monitor.

    GraphicsEnvironment ge = GraphicsEnvironment.
    getLocalGraphicsEnvironment();
    GraphicsDevice[] gs = ge.getScreenDevices();
    for (int j = 0; j < gs.length; j++) { 
       GraphicsDevice gd = gs[j];
       System.out.println("ID#: " + gd.getDeviceID());
    }
    

    Gives this for me:

    ID#: \Display0
    ID#: \Display1
    

    Remember, that sometimes secondary monitor can be to the left of the primary. In this case, it will have negative bounds.

    In order to determine whole bounds of the screen area, you can use an example, provided at the JDK Javadocs site: GraphicsConfiguration