Dropdown isOpen

Okay, I would probably do something like this, where you have a dictionary which defines the component name and the attribute that you want to change for that component.

Library Script:

from javax.swing.event import PopupMenuListener
class PopupMenuAdapter(PopupMenuListener):
    def __init__(self, callback):
        self.callback = callback

    def popupMenuCanceled(self, event):
        self.callback(event.source.popupVisible)

    def popupMenuWillBecomeVisible(self,event):
        self.callback(event.source.popupVisible)

    def popupMenuWillBecomeInvisible(self, event):
        self.callback(event.source.popupVisible)

def popupVisibleChange(event,compAttrs,popupVisible):
    for comp,attr in compAttrs.iteritems:
        setatter(event.source.parent.getComponent(comp),attr,popupVisible)

def addPopupChangeListener(event):
    comps = {'Switch 2POS Buttons':'visible'}

    def callback(popupVisible):
        library.popupVisibleChange(event,comps,popupVisible)

    listener = PopupMenuAdapter(callback)
    event.source.addPopupMenuListener(listener)

Property Change Script

if event.propertyVname == 'componentRunning' and event.newValue:
    library.addPopupChangeListener(event)
1 Like

What if the desired component is in a different location in the window?

Seems like I should just have a custom prop on the dropdown that toggles when it is open and bind any component properties to that.

You could always use the .getComponentForPath() function from the root container.

Certainly a viable approach, and also makes the script less variable.

Window method getComponentForPath()

ok, so the dropdown now has a custom BOOL property dropdownIsOpen

Here's the library script:

from javax.swing.event import PopupMenuListener
class DropdownIsOpenListener(PopupMenuListener):
  def __init__(self, target):
     self.target = target
  def popupMenuCanceled(self, event):
     self.target.dropdownIsOpen = False
  
  def popupMenuWillBecomeVisible(self, event):
     self.target.dropdownIsOpen = True
  
  def popupMenuWillBecomeInvisible(self, event):
     self.target.dropdownIsOpen = False

here's the call from the dropdown property change script:

if event.propertyName == 'componentRunning' and event.newValue:
	listener = GUI.DropdownIsOpenListener(event.source)
	event.source.addPopupMenuListener(listener)

the component that I want to disable when the dropdown is open is bound to (not) dropdownIsOpen but it does not change.

I don't see any issue with your code that would prevent the expected result.

Where are you testing this? If it's in the designer, the component running event won't occur unless preview mode is on prior to the window being opened.

If there is an issue with the listener, it will fail silently. Check for red letter errors in the diagnostic console.

Thanks for the tip! It is working if I enable preview mode before opening the window in the designer.

Thanks everybody!

1 Like

Why does adding a doc string after the class declaration cause a syntax error? I'd like to add some documentation to the custom listener.

image

edit: nevermind, just an indentation issue:
image

1 Like