There we go! I replaced it in the setLocation
call, but nowhere else. That's perfect.
# Import the necessary Javax Swing classes
from javax.swing import JFrame,SwingUtilities
def moveWindowRelative(window):
'''
Moves a popup window to a position
'''
# Find the nearest ancestor JFrame of the window component which will be the parent window that the popup was opened from
parentJFrame = SwingUtilities.getAncestorOfClass(JFrame, window)
# Set a margin value for positioning the window
parentJFrameMargin = 30
#initially assume a window move is required
moveRequired = True
# Try to get the mouse position relative to the parent JFrame
try:
windowX = parentJFrame.getMousePosition(True).x
windowY = parentJFrame.getMousePosition(True).y
# If an exception occurs (e.g., mouse position not available), do not move the window
except:
moveRequired = False
if moveRequired:
# Ensure that the window is within the boundaries of the parent JFrame
if windowX < parentJFrameMargin:
windowX = parentJFrameMargin
elif windowX + window.width > parentJFrame.width - parentJFrameMargin:
windowX = parentJFrame.width - window.width - parentJFrameMargin
if windowY < parentJFrameMargin:
windowY = parentJFrameMargin
elif windowY + window.height > parentJFrame.height - parentJFrameMargin:
windowY = parentJFrame.height - window.height - parentJFrameMargin
# Set the new location of the window based on the calculated positions
window.setLocation(windowX, windowY)