MouseClicked event on template instance not working

I have a template with only Text Field component in it. I want a script to run when I click on this template’s instance. I have put a script on MouseClicked event of this template’s instance. There is no script on any event of the template or the Text Field.

Reading through other posts, I understood that Java Swing triggers ‘innermost’ script only. But with no script in template, I thought the script on template instance should run.

Am I missing something?

What script is on your MouseClicked? Lets make sure there’s nothing wrong there first.

Script contains function to open a popup system.nav.openWindow(windowPath). I also tried with simple print statement but got the same result.

When you’re clicking the template, are you clicking inside the text box, or just the general area?

I’m pretty sure the text field component has a built-in listener for mouse clicks, in order to gain focus. So it would never pass clicks to a parent.

1 Like

I am clicking inside the text box. Size of text field and template is kept same.

Yes, text field component has a mouse clicked event. If I write the same script on text field (inside template), then it gets triggered when I click on the instance.
So, does it mean that what I am trying to do (having script on mouse clicked event of instance instead of inside template) is not possible?

Yes, likely not possible.

Instead, consider writing what you need in a custom method on the instance, and have the label delegate to that. Something like this:

event.source.parent.parent.parent.someCustomMethod(event)

{ Adjust number of .parent references for your situation. }

@pturmel Thank you for your reply. Sorry for my late response.
There is no custom method in instance like we have on Button for example
image
Am I looking at correct place?

For our project we have now written the script on Text Field (inside template) since it will be common for all. But it is interesting to know when events on instance are triggered since there will always be some component within template with same events with priority over instance events.

Well, shoot. I hadn’t noticed that. The times I’ve used this technique, I was using the Template Repeater or the Template Canvas.

As a work-around, place the instance in a container and use the container for your custom method.