When constructing a new JWindow or JFrame is it possible to right align the edge of the window to the visible edge of a monitor?
This will be used on Windows systems with multiple monitors. Ideally I would like to have the right edge of a new window snap to the right edge of the right most screen. Currently I am doing this by obtaining the GraphicsDevices, sorting, comparing Point location + window size and adjusting the location to 'move' the window before display if I detect if the right edge of the window is out of bounds of the visible screen area. I did this before finding this question which is essentially the same idea (How to know if a JFrame is on screen in a multi screen environment). This accomplishes the goal. I know the SSCCE advertisers would like me to post what I am already doing, and there is already one in the other question if interested, but my intent is to ask if there is a much simpler / more native way to obtain these results? As a side note it take 2.6ms the first call and .066ms each additional call. Originally my nanosecond to millisecond math was wrong, so performance is not a factor now, but I would still be curious if there is a better way to do this.
Here are some example debug sysouts if interested. First two on left of first monitor, third test on right side of second monitor:
Last Monitor Rectangle: java.awt.Rectangle[x=1920,y=150,width=1680,height=1050] Point to compare : java.awt.Point[x=20,y=100], windowWidth:1270 rightBoundOfMonitor : 3600.0 rightBoundOfNewWindow : 1290.0 In bounds Adjustment time : 2536893ns, 2.536893ms Last Monitor Rectangle: java.awt.Rectangle[x=1920,y=150,width=1680,height=1050] Point to compare : java.awt.Point[x=20,y=100], windowWidth:1270 rightBoundOfMonitor : 3600.0 rightBoundOfNewWindow : 1290.0 In bounds Adjustment time : 65880ns, 0.06588ms Last Monitor Rectangle: java.awt.Rectangle[x=1920,y=150,width=1680,height=1050] Point to compare : java.awt.Point[x=3147,y=315], windowWidth:1270 rightBoundOfMonitor : 3600.0 rightBoundOfNewWindow : 4417.0 Out of bounds Adjustment time : 152949ns, 0.152949ms
EDIT: Performance was not nearly as much of an issue as I initially thought. (1000000 ns in a ms, not 1000). The first call to GraphicsDevice takes ~2.5ms, which is acceptable, subsequent calls to determine position are much quicker. It appears that currently there are no API calls to do this easier than I came up with, or what others have derived either.