Search code examples
javaswingjframelocationmonitor

How does default location work? / What is the default monitor?


I'm working on some app that gets the users width for their multiple monitors, assume they don't have any monitors on top, and then provide them with multiple buttons/frames, so that they can black out whichever monitors they wish.

MY ISSUE All's well and good, apart from location compatability stuff. If someone has 4 1920 width monitors, how can I position my reference/starting frames at the leftest monitor, so that I can say "proceed 1920 pixels, make a frame, ..."? You'd think "Use setLocationtoNull, but how does that work?? You'd think it's simply the middle monitor, length/2; but what is the middle monitor!? It can't be totalScreensLength/2, because that'd mean the program is split in half between the monitors! (so it's most likely the middle of the main manitor). And not using any setLocations, the program is at the top left of the main monitor.... BUT WHAT IS THE MAIN MONITOR??

If I knew how java figured which monitor was "main", that would solve my issue (and no, knowing merely the main monitors resolution via Toolkit.getScreenSize() would not be enough info).

    //creating invis black frames
for(int c=0;c<inArr.length;c++){
    int xLoc=0;
    for(int i=0;i<c;i++){
/**/    xLoc+=resList[i];
    }System.out.println(xLoc);

    JFrame blackOut=new JFrame();
    blkList.add(blackOut);
    blackOut.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    blackOut.getContentPane().setBackground(Color.BLACK);
    blackOut.setUndecorated(true);
                    
    blackOut.setSize(resList[c],5000);
/**/blackOut.setLocation(xLoc-1280,-2000);
                    
    blackOut.setVisible(false);
}

This is the code that makes the program work, because it's hardcoded for my setup of 1280,1920,1440. The issue is at blackOut.setLocation(xLoc-1280,-2000); I must know where exactly the starting frame would be, so I can move the frame to the left, so 'scaling' works. This is what's happening here: the frame starts at the top left of my middle, 1920 monitor, so I move the frame 1280 pixels left so it is at the top left of my leftest monitor.

Sorry if this is too verbose, just want to give enough detail. Also, recently discovered this and went into its documentation, but I don't understand how I could use the data, and am unsure if it's able to provide me the information necessary (know what monitor is main, and be able to differentiate monitors, even if they're the same resolution)


Solution

  • This might be a partial answer. From the linked discovery of yours, you can get the default configuration and bounds of each device by calling graphicsDevice.getDefaultConfiguration().getBounds(). This includes the coordinates / offset of each screen as well as the individual screen size. In the implementation for GraphicsEnvironment.getScreenDevices() by SunGraphicsEnvironment screen devices are created by iterating over the number of screens and creating a device for each index - the index is passed to each device.