Popup Screen location

Hello, I would like to know how to have a popup window displayed near the component that I clicked on the main window.

I’ve read about the event.x and event.y properties but, they seem to give me a position relative to the component that was clicked, not relative to the window the component is on. For example, if I have a component that is 20x20 and is at position 100,100 in the root container, then event.x and event.y will give me a number between 0,0 and 20,20 - depending on where I click in the component, which has nothing to do with where the component is in the root container. The component I’m using is from a template - not sure if that makes it any more difficult. How can I read the components x,y positions within the root container.

Sorry if this has been asked before, I’ve already searched the forum but could not find the answer. Thank you…

You’re right. event.getX() and event.getY() will give coordinates it within the component.

Using event.source.getX() and event.source.getY(), however, will give the location of the component. :smiley:

Hope this helps!

Thanks for the help, but that doesn’t work for me. I get the following error message

[code][size=85]Traceback (most recent call last):
File “event:mouseClicked”, line 11, in
AttributeError: ‘com.inductiveautomation.factorypmi.application.com’ object has no attribute ‘getx’

at org.python.core.PyException.fillInStackTrace(PyException.java:70)
at java.lang.Throwable.<init>(Throwable.java:181)
at java.lang.Exception.<init>(Unknown Source)
at java.lang.RuntimeException.<init>(Unknown Source)
at org.python.core.PyException.<init>(PyException.java:46)
at org.python.core.PyException.<init>(PyException.java:43)
at org.python.core.PyException.<init>(PyException.java:61)
at org.python.core.Py.AttributeError(Py.java:173)
at org.python.core.PyObject.noAttributeError(PyObject.java:930)
at org.python.core.PyObject.object___getattribute__(PyObject.java:3692)
at org.python.core.PyObject$object___getattribute___exposer.__call__(Unknown Source)
at org.python.core.Deriveds.__findattr_ex__(Deriveds.java:59)
at org.python.core.PyObjectDerived.__findattr_ex__(PyObjectDerived.java:983)
at com.inductiveautomation.factorypmi.application.script.PyComponentWrapper.__findattr_ex__(PyComponentWrapper.java:101)
at org.python.core.PyObject.__getattr__(PyObject.java:923)
at org.python.pycode._pyx24.f$0(<event:mouseClicked>:15)
at org.python.pycode._pyx24.call_function(<event:mouseClicked>)
at org.python.core.PyTableCode.call(PyTableCode.java:165)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.Py.runCode(Py.java:1275)
at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:539)
at com.inductiveautomation.factorypmi.application.binding.action.ActionAdapter.runActions(ActionAdapter.java:155)
at com.inductiveautomation.factorypmi.application.binding.action.ActionAdapter.invoke(ActionAdapter.java:266)
at com.inductiveautomation.factorypmi.application.binding.action.RelayInvocationHandler.invoke(RelayInvocationHandler.java:55)
at $Proxy16.mouseClicked(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Ignition v7.5.4 (b1206)
Java: Sun Microsystems Inc. 1.6.0_31
[/size][/code]

I also tried event.source.x and event.source.y but, I always get a result of 0,0

The X and Y need to be capitalized…

Here is sample code I pasted into the mouseClicked event of a button. It changes the button’s text to display:

  • The coordinates of the mouseClicked event
  • The location of the button within the container
  • The size of the button

EDIT: Clean up after forum migration

x1=event.getX()
y1=event.getY()
x2=event.source.getX()
y2=event.source.getY()
s=event.source.getSize()
x3=s.width
y3=s.height

event.source.text="<html><center>Component: " + str(x1) + "," + str(y1) + "<br>Container: " + str(x2) + "," + str(y2)+"<br>Size: " + str(x3) + "," + str(y3)

Hope this helps!

You can copy this code for the button itself, and paste the code directly into the container. That’s just cool stuff… 8)

This button will also give the location on the screen. :slight_smile: You can then look at the script to see what I did.

EDIT: Clean up after forum migration

<objects timestamp="Wed May 01 11:45:07 EDT 2013" fpmi.archive.type="components" framework.version="7.2.11.209" fpmi.version="4.2.7.109" >
<arraylist len="1">
	<o cls="com.inductiveautomation.factorypmi.application.binding.action.ActionAdapter">
		<o-c m="setEventSet" s="1;java.beans.EventSetDescriptor">
			<o cls="java.beans.EventSetDescriptor">
				<o-ctor s="5;str;java.lang.Class;[java.beans.MethodDescriptor;java.lang.reflect.Method;java.lang.reflect.Method">
					<str>mouse</str>
					<class>java.awt.event.MouseListener</class>
					<array cls="java.beans.MethodDescriptor" len="5">
						<o id="0" cls="java.beans.MethodDescriptor">
							<o-ctor s="2;java.lang.reflect.Method;[java.beans.ParameterDescriptor">
								<method>java.awt.event.MouseListener|mouseClicked|1;java.awt.event.MouseEvent</method>
								<null/>
							</o-ctor>
						</o>
						<o cls="java.beans.MethodDescriptor">
							<o-ctor s="2;java.lang.reflect.Method;[java.beans.ParameterDescriptor">
								<method>java.awt.event.MouseListener|mouseEntered|1;java.awt.event.MouseEvent</method>
								<null/>
							</o-ctor>
						</o>
						<o cls="java.beans.MethodDescriptor">
							<o-ctor s="2;java.lang.reflect.Method;[java.beans.ParameterDescriptor">
								<method>java.awt.event.MouseListener|mouseExited|1;java.awt.event.MouseEvent</method>
								<null/>
							</o-ctor>
						</o>
						<o cls="java.beans.MethodDescriptor">
							<o-ctor s="2;java.lang.reflect.Method;[java.beans.ParameterDescriptor">
								<method>java.awt.event.MouseListener|mousePressed|1;java.awt.event.MouseEvent</method>
								<null/>
							</o-ctor>
						</o>
						<o cls="java.beans.MethodDescriptor">
							<o-ctor s="2;java.lang.reflect.Method;[java.beans.ParameterDescriptor">
								<method>java.awt.event.MouseListener|mouseReleased|1;java.awt.event.MouseEvent</method>
								<null/>
							</o-ctor>
						</o>
					</array>
					<method>java.awt.Component|addMouseListener|1;java.awt.event.MouseListener</method>
					<method>java.awt.Component|removeMouseListener|1;java.awt.event.MouseListener</method>
				</o-ctor>
			</o>
		</o-c>
		<o-c m="setJythonCode" s="1;str"><str># This script was generated automatically by the tag set
# script builder. You may modify this script, but if you do,
# you will not be able to use the tag set builder to update
# this script without overwriting your changes.

x1=event.getX()
y1=event.getY()
x2=event.source.getX()
y2=event.source.getY()
x3=event.source.getLocationOnScreen().x
y3=event.source.getLocationOnScreen().y
w1=event.source.getSize().width
h1=event.source.getSize().height

event.source.text=&quot;&lt;html&gt;&lt;center&gt;Click in Comp.: &quot; + str(x1) + &quot;,&quot; + str(y1) + &quot;&lt;br&gt;Container Loc.: &quot; + str(x2) + &quot;,&quot; + str(y2) +&quot;&lt;br&gt;Screen Loc.: &quot; + str(x3) + &quot;,&quot; + str(y3) +&quot;&lt;br&gt;Size: &quot; + str(w1) + &quot;,&quot; + str(h1)</str></o-c>
		<o-c m="setMethodDescriptor" s="1;java.beans.MethodDescriptor"><ref>0</ref></o-c>
		<o-c m="setTarget" s="1;java.awt.Component">
			<c id="1" cls="com.inductiveautomation.factorypmi.application.components.PMIButton">
				<c-comm>
					<dim>279;78</dim>
					<rect>158;113;279;78</rect>
					<str>Button</str>
					<lc>158;113;16;0;0.043010753;0.15384616</lc>
				</c-comm>
				<c-c m="setText" s="1;str"><str>&lt;html&gt;&lt;center&gt;Click in Comp.: 236,49&lt;br&gt;Container Loc.: 15,20&lt;br&gt;Screen Loc.: 544,212&lt;br&gt;Size: 279,78</str></c-c>
			</c>
		</o-c>
	</o>
</arraylist>
<ref>1</ref>
</objects>

JordanCClark - thanks works perfect. I guess the method I was looking for is “.getLocationOnScreen().x”

I really did search Google, forums and Ignition help, to find this type of function but to no avail. Can you point me to a better way to search for these kinds of “hidden” methods/properties - so I can solve my own problems in the future?

Well, I’ll admit it’s sometimes a TIAS (Try It And See) method of programming. I certainly didn’t get it on the first try. I’m not necessarily the best programmer here, I’m just more tenacious than most! :laughing:

What I can tell you is that things relating to components are all Java Swing methods, so you can learn a bit through the javadocs.

Google can really be your friend. A search may go something like “java swing find location of a component”.

Some of my inspirations comes from the StackOverflow site, but a lot is from others’ posts here on the forum that will jog something in the back of by brain.

Wish I could have a better answer for you. In the meantime, don’t be afraid to post questions here on the forum. We have a great group of people here ready to lend a hand. :thumb_left:

Here is the code you are looking for:

container_X = event.source.getLocationOnScreen().x
container_Y = event.source.getLocationOnScreen().y

window_X = system.gui.getParentWindow(event).getLocationOnScreen().x
window_Y = system.gui.getParentWindow(event).getLocationOnScreen().y

source_relX = container_X - window_X + event.source.relX
source_relY = container_Y - window_Y + event.source.relY

print source_relX
print source_relY

system.gui.transform(system.gui.getParentWindow(event).getRootContainer().getComponent(‘Rectangle’),source_relX + event.source.relWidth, source_relY)

Create 2 Rectangles (Rectangle1, Rectangle2)
Put Rectangle2 on the Root Container
Put Rectangle1 anywhere, in a container or container in a container 3 deep…

Copy this code to Rectangle 1 mouseClicked Event Handler:

container_X = event.source.getLocationOnScreen().x
container_Y = event.source.getLocationOnScreen().y

window_X = system.gui.getParentWindow(event).getLocationOnScreen().x
window_Y = system.gui.getParentWindow(event).getLocationOnScreen().y

source_relX = container_X - window_X + event.source.relX
source_relY = container_Y - window_Y + event.source.relY

print source_relX
print source_relY

This code will move Rectangle2 next to Rectangle1 no mater where it is!

system.gui.transform(system.gui.getParentWindow(event).getRootContainer().getComponent(‘Rectangle2’),source_relX + event.source.relWidth, source_relY)

Don’t forget to enclose your code in triple backquotes ( ``` ), my friend. :slight_smile:

Hi Jordan,
Why enclose code in triple backquotes ( ``` )?
Bruce

To cause the forum's code-formatter to make it readable for the rest of us. Place them on lines by themselves above and below your code. More information here:

Thanks for the clarity!