Why can't custom properties and methods be accessed from a component that is obtained using SwingUtilities.getAncestorOfClass?

Thanks for the hint. Wrapping the swing utility call in the PyComponentWrapper returns the object with the custom properties accessible.

Just for completeness and clarity; the recursive approach always works for returning a component with its custom properties.

Recursive example to get the parent container of a component:

from com.inductiveautomation.factorypmi.application.components import BasicContainer
def getParentContainer(component):
	if isinstance(component.parent, BasicContainer):
		return component.parent
	else:
		parentContainer = getParentContainer(component.parent)
		if parentContainer is not None:
			return parentContainer
parentContainer = getParentContainer(event.source)

The getAncestorOfClass approach returns the expected component and takes far fewer lines of code, but all custom properties are lost:

Basic SwingUtilities Example:

from com.inductiveautomation.factorypmi.application.components import BasicContainer
from javax.swing import SwingUtilities
parentContainer = SwingUtilities.getAncestorOfClass(BasicContainer, event.source)

By simply wrapping the swing utility call in a pyComponentWrapper call, the component is returned with the custom properties just like the recursive approach.

pyComponentWrapper with SwingUtilities example:

from com.inductiveautomation.factorypmi.application.components import BasicContainer
from com.inductiveautomation.factorypmi.application.script import PyComponentWrapper
from javax.swing import SwingUtilities
parentContainer = PyComponentWrapper(SwingUtilities.getAncestorOfClass(BasicContainer, event.source))

It's only one more line of code, and it's still cleaner looking than building a recursive function.

2 Likes