I suppose it depends on what the screen index is being used for. When I position windows in multi monitor setups, I want the screens to be sequentially indexed from left to right and top to bottom because my windows get covered up, and it makes it easier to find a specific instance in a specific monitor when tabbing. The problem is, screen indexes don't often work that way.
Here's a variation on a script I developed that will return an index based upon which monitor the parent window is in from left to right, top to bottom:
# Import:
# ...GraphicsEnvironment for mapping the screens as they are in reality
# ...JFrame and SwingUtilities for getting the ancestor JFrame of a given component
from java.awt import GraphicsEnvironment
from javax.swing import JFrame, SwingUtilities
def getScreenIndex(component):
# Get the screen bounds from the graphics environment and sort them from left to right, top to bottom
screens = sorted([device.defaultConfiguration.bounds for device in GraphicsEnvironment.getLocalGraphicsEnvironment().screenDevices], key=lambda screen: (screen.x, screen.y))
parentFrame = SwingUtilities.getAncestorOfClass(JFrame, component)
for index, screen in enumerate(screens):
if screen.contains(parentFrame.location()):
return index
# Call the function from any component in any window and do something with the returned screen index
print getScreenIndex(event.source)