# Open Template Popup at Mouse Location

**URL:** <https://forum.inductiveautomation.com/t/open-template-popup-at-mouse-location/62678>\
**Category:** Ignition\
**Tags:** vision\
**Created:** [August 4, 2022, 2:59pm UTC](https://forum.inductiveautomation.com/t/open-template-popup-at-mouse-location/62678 "2022-08-04T14:59:54Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Taylor.Johnson](https://avatars.discourse-cdn.com/v4/letter/t/c68b51/32.png) [@Taylor.Johnson](https://forum.inductiveautomation.com/u/Taylor.Johnson)\
**Post date:** [August 4, 2022, 2:59pm UTC](https://forum.inductiveautomation.com/t/open-template-popup-at-mouse-location/62678/1 "2022-08-04T14:59:54Z")

</div>

Good morning, I’m having issues with a script and wonder if someone had any ideas.

What I have is a template with a button, when that button is pressed it passes parameters from the UDT to a popup window and opens said popup window at the mouse cursor location.

I tested the below script with just a normal button on a screen and it works, but when I try to do it with a templates button the window does shift over some in the right direction, but not to the mouse cursor. I assume this has something to do with the button being inside of the template container instead of the root container?

```auto
mouseX = event.x
mouseY = event.y
compX = event.source.getX()
compY = event.source.getY()
parWindow = system.gui.getParentWindow(event)
windowX = parWindow.getX()
windowY = parWindow.getY()
param1 = event.source.parent.MotorNumber

window = system.nav.openWindow('Popup Windows/PRP_Motor_FVNR', {'MotorNumber' : param1})

window.setLocation(mouseX+compX+windowX,mouseY+compY+windowY)

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 4, 2022, 3:31pm UTC](https://forum.inductiveautomation.com/t/open-template-popup-at-mouse-location/62678/2 "2022-08-04T15:31:46Z")

</div>

Correct, the x & y of the event are relative to the component of the event. To obtain the placement on the screen, you’ll need to traverse the chain of parents of the component, adding their offsets. You are missing the position of the template within the window.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 4, 2022, 3:50pm UTC](https://forum.inductiveautomation.com/t/open-template-popup-at-mouse-location/62678/3 "2022-08-04T15:50:41Z")

</div>

SwingUtilities may be helpful here:  
[https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/SwingUtilities.html#convertPointFromScreen(java.awt.Point,java.awt.Component)](https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/SwingUtilities.html#convertPointFromScreen(java.awt.Point,java.awt.Component))  
[https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/SwingUtilities.html#convertPointToScreen(java.awt.Point,java.awt.Component)](https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/SwingUtilities.html#convertPointToScreen(java.awt.Point,java.awt.Component))

---

<div class="post-metadata">

**Author:** ![Taylor.Johnson](https://avatars.discourse-cdn.com/v4/letter/t/c68b51/32.png) [@Taylor.Johnson](https://forum.inductiveautomation.com/u/Taylor.Johnson)\
**Post date:** [August 4, 2022, 4:05pm UTC](https://forum.inductiveautomation.com/t/open-template-popup-at-mouse-location/62678/4 "2022-08-04T16:05:30Z")

</div>

Thanks both of you! I’d been brute forcing it from some forum posts and what I thought would work - but the SwingUtilities documents will be helpful

---

<div class="post-metadata">

**Author:** ![Taylor.Johnson](https://avatars.discourse-cdn.com/v4/letter/t/c68b51/32.png) [@Taylor.Johnson](https://forum.inductiveautomation.com/u/Taylor.Johnson)\
**Post date:** [August 4, 2022, 7:03pm UTC](https://forum.inductiveautomation.com/t/open-template-popup-at-mouse-location/62678/5 "2022-08-04T19:03:06Z")

</div>

So I was able to find another forum post trying to solve a similar problem and using the convert point to screen swingutility - the script functions. However, the script fails if I have a second vision window open on another monitor. It will only function for the screen that is located at the top-left monitor position. For multi-monitor it fails and trys to open the pop-up offscreen. Any advice?

```auto

def setRelativeLocation(event, window, compRelX, compRelY):
	windowX = window.getX()
	windowY = window.getY()
	
	screenRelX, screenRelY = system.gui.convertPointToScreen(compRelX, compRelY, event)

	window.setLocation(screenRelX, screenRelY)

param1 = event.source.parent.MotorNumber

win = system.nav.openWindow('Popup Windows/PRP_Motor_FVNR', {'MotorNumber' : param1})
setRelativeLocation(event, win, event.x, event.y)

```

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [August 4, 2022, 7:14pm UTC](https://forum.inductiveautomation.com/t/open-template-popup-at-mouse-location/62678/6 "2022-08-04T19:14:00Z")

</div>

You don’t want screen coordinates. You want coordinates within the frame containing the parent window. Consider experimenting with the more generic [convertPoint()](https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/SwingUtilities.html#convertPoint(java.awt.Component,int,int,java.awt.Component)) method, where `source` is your template and `dest` is your parent window’s parent.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [August 9, 2023, 1:06am UTC](https://forum.inductiveautomation.com/t/open-template-popup-at-mouse-location/62678/7 "2023-08-09T01:06:53Z")

</div>

For posterity.. (i.e. for future me), you can use this:

```auto
from javax.swing import SwingUtilities
window = system.nav.openWindow('Popup Path', {})
newPoint = SwingUtilities.convertPoint(event.source, event.x, event.y, system.gui.getParentWindow(event).rootContainer)
window.setLocation(newPoint.x, newPoint.y)

```

Thanks Phil for the massive prompt

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 9, 2023, 2:14am UTC](https://forum.inductiveautomation.com/t/open-template-popup-at-mouse-location/62678/8 "2023-08-09T02:14:39Z")

</div>

For posterity:  
[https://docs.inductiveautomation.com/display/DOC81/system.gui.convertPointToScreen](https://docs.inductiveautomation.com/display/DOC81/system.gui.convertPointToScreen)

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [August 9, 2023, 2:42am UTC](https://forum.inductiveautomation.com/t/open-template-popup-at-mouse-location/62678/9 "2023-08-09T02:42:45Z")

</div>

Pointtoscreen isn't helpful in most cases though. I tried it with my 3 monitors and my new point was off the page ☹ it also doesn't help for clients that are not fullscreen if displayed on a single monitor system, since the point still references the top left of the monitor, not the client

---

<div class="post-metadata">

**Author:** ![Taylor.Johnson](https://avatars.discourse-cdn.com/v4/letter/t/c68b51/32.png) [@Taylor.Johnson](https://forum.inductiveautomation.com/u/Taylor.Johnson)\
**Post date:** [August 9, 2023, 2:42pm UTC](https://forum.inductiveautomation.com/t/open-template-popup-at-mouse-location/62678/10 "2023-08-09T14:42:18Z")

</div>

You and Phil are the men! I tried using PointToScreen and it broke bc of multiple monitors - this worked excellently!

---

<div class="post-metadata">

**Author:** ![PFollansbee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pfollansbee/32/46440_2.png) [@PFollansbee](https://forum.inductiveautomation.com/u/PFollansbee)\
**Post date:** [September 11, 2023, 11:04pm UTC](https://forum.inductiveautomation.com/t/open-template-popup-at-mouse-location/62678/11 "2023-09-11T23:04:15Z")

</div>

I implemented something similar to this with

```auto
from javax.swing import SwingUtilities
from com.inductiveautomation.factorypmi.application import VisionDesktop
appWindow = SwingUtilities.getAncestorOfClass(VisionDesktop, event.source)
window.setLocation(SwingUtilities.convertPoint(event.source, event.x, event.y, appWindow))

```

Seems to work well enough, but wondering if I should change it to what is shown here.
