java - Get size of a secondary monitor -


this question has answer here:

i seem struggle size of more 1 monitor in java;

so made little window supposed display screen's dimensions, works fine primary monitor, able determine size of monitor it's on, used getlocation() know jframe is, don't know how size of monitor, can primary one, or total size.

you need down graphicsenvironment give access graphicsdevice available on system.

essentially there, need loop through each graphicsdevice , test see if window within bounds of given graphicsdevice

the fun part, if window spans across multiple screens...

public static graphicsdevice getgraphicsdevice(component comp) {      graphicsdevice device = null;     graphicsenvironment ge = graphicsenvironment.getlocalgraphicsenvironment();     graphicsdevice lstgds[] = ge.getscreendevices();     arraylist<graphicsdevice> lstdevices = new arraylist<graphicsdevice>(lstgds.length);      if (comp != null && comp.isvisible()) {         rectangle parentbounds = comp.getbounds();         /*          * if component not window, need find location on          * screen...          */         if (!(comp instanceof window)) {             point p = new point(0, 0);             swingutilities.convertpointtoscreen(p, comp);             parentbounds.setlocation(p);         }          // devices window intersects (ie window might expand across multiple screens)         (graphicsdevice gd : lstgds) {             graphicsconfiguration gc = gd.getdefaultconfiguration();             rectangle screenbounds = gc.getbounds();             if (screenbounds.intersects(parentbounds)) {                 lstdevices.add(gd);             }         }          // if there 1 device listed, return it...         // otherwise, if there more 1 device, find device         // window "mostly" on         if (lstdevices.size() == 1) {             device = lstdevices.get(0);         } else if (lstdevices.size() > 1) {             graphicsdevice gdmost = null;             float maxarea = 0;             (graphicsdevice gd : lstdevices) {                 int width = 0;                 int height = 0;                 graphicsconfiguration gc = gd.getdefaultconfiguration();                 rectangle bounds = gc.getbounds();                 rectangle2d intbounds = bounds.createintersection(parentbounds);                 float perarea = (float) ((intbounds.getwidth() * intbounds.getheight()) / (parentbounds.width * parentbounds.height));                 if (perarea > maxarea) {                     maxarea = perarea;                     gdmost = gd;                 }             }              if (gdmost != null) {                 device = gdmost;             }         }     }      return device; }