Button template

Hello,

I’m simply trying to make a button template so I have the same type of button everywhere in my project, so if I change the color it changes every button

The problem is, when I try to make a mouseClicked event on an instance of the template, it doesn’t work because when you click the instance, the event called is the event of the button from inside the template

Every button could do a different action, like system.nav.swapTo or set a tag to a value or set multiple tags to different values, etc.

I tried this :

I got the instance that is called “btnTest” and tried to call the event “mouseClicked” but nothing works

This is my instance :

I would recommend, instead of using a template, is to bind the colors to tags. After you get it the way you want it, you can add it to a custom palette.

4 Likes

With the template I can change everything, color, text, font, size, etc, even change de design
With the palette and binding it would demand a lot of tags

Bind them to the same tags for color.

Example coming.

Put this inside the event you want to handle for your instances. We used mousePressed for our templates:

from javax.swing import SwingUtilities
template = event.source.parent.parent
convertedEvent = SwingUtilities.convertMouseEvent(event.source,event,template)
template.dispatchEvent(convertedEvent)

This code should be put on the template definition not the template instance FYI

2 Likes

ButtonStyle_2022-08-09_1222.zip (8.8 KB)

Example has a button with a custom property called ‘style’. Colors are set through the style customizer of the button driven by the ‘style’ custom property. the value of ‘style’ is set via different mouse events of the button. The dataset for the styles is bound to a client tag.

1 Like

Thanks! I could use it for other projects

I found the solution

So if the instance has a script in mouseClicked, the button inside the template should have this script in mouseClicked:

from java.awt.event import MouseEvent evt = MouseEvent(event.source.parent.parent,event.getID(),event.getWhen(),event.getModifiers(),event.x,event.y,event.clickCount,event.popupTrigger,event.button) event.source.parent.parent.dispatchEvent(evt)