Thanks for the script! I'll try to adapt this for my use.
I have one call that opens the popup and another that repositions the popup, all within the onActionPerformed
for a button, like so:
if event.source.parent.parent.clickable:
window = Nav.popup1(event)
Nav.moveWindowRelative(event, window)
def popup1(event):
'''
Calls pop with path defined on the root container
Args:
event (obj) : event handler (typically mouse click or button action)
Returns:
window (obj) : window that was opened
'''
#get component path from root container
param1 = event.source.parent.parent.path
#call popup with path
window = system.nav.openWindowInstance('Popups/PopUp', {'path' : param1})
system.nav.centerWindow(window)
return window
If I use your code as-is
def moveWindowRelative(event, window):
from javax.swing import JFrame,SwingUtilities
# Get an unambiguous reference to the 'popupWindow' component from the event source
popupWindow = event.source
# Find the nearest ancestor JFrame of the 'popupWindow' component which will be the parent window that the popup was opened from
parentJFrame = SwingUtilities.getAncestorOfClass(JFrame, popupWindow)
# Set a margin value for positioning the 'popupWindow'
parentJFrameMargin = 100
# Try to get the mouse position relative to the 'parent' JFrame
try:
popupWindowX = parentJFrame.getMousePosition(True).x
popupWindowY = parentJFrame.getMousePosition(True).y
# If an exception occurs (e.g., mouse position not available), set default values of (0,0) [Upper left corner] plus the specified margin
except:
popupWindowX = parentJFrameMargin
popupWindowY = parentJFrameMargin
# Ensure that the 'popupWindow' is within the boundaries of the 'parent' JFrame
if popupWindowX < parentJFrameMargin:
popupWindowX = parentJFrameMargin
elif popupWindowX + popupWindow.width > parentJFrame.width - parentJFrameMargin:
popupWindowX = parentJFrame.width - popupWindow.width - parentJFrameMargin
if popupWindowY < parentJFrameMargin:
popupWindowY = parentJFrameMargin
elif popupWindowY + popupWindow.height > parentJFrame.height - parentJFrameMargin:
popupWindowY = parentJFrame.height - popupWindow.height - parentJFrameMargin
# Set the new location of the 'popupWindow' based on the calculated positions
popupWindow.setLocation(popupWindowX, popupWindowY)
The window always just opens at 0,0.
If I replace the las popupWindow.setLocation with my window.setLocation it still opens off the screen.
I'll try and do some troubleshooting with the console.
*edit: so, when I print the JFrame that is found it looks like this:
com.inductiveautomation.ignition.designer.IgnitionDesigner[frame1,1912,-8,1936x1056,invalid,layout=java.awt.BorderLayout,title=Grain_SCC - Ignition-LPTP134 - Ignition Designer,resizable,maximized,defaultCloseOperation=DO_NOTHING_ON_CLOSE,rootPane=javax.swing.JRootPane[,8,31,1920x1017,invalid,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.synth.SynthBorder@7071c1,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]