Vaadin-on-Kotlin: Updating menubutton value using push

Hi,
What is the best approach to update the menubutton value i.e. 3 dynamically using push notification in vaadin on Kotlin?

val content = valoMenu { appTitle = "<h3>Vaadin-on-Kotlin <strong>Sample App</strong></h3>" userMenu { item("John Doe", ClassResource("profilepic300px.jpg")) { item("Edit Profile") item("Preferences") addSeparator() item("Sign Out") } } menuButton("Welcome", VaadinIcons.MENU, "3", WelcomeView::class.java) menuButton("CRUD Demo", VaadinIcons.NOTEBOOK, view = CrudView::class.java) verticalLayout { setSizeFull(); expandRatio = 1f; primaryStyleName = "valo-content" this@valoMenu.viewPlaceholder = cssLayout { addStyleName("v-scrollable"); setSizeFull(); expandRatio = 1f } statusTicker = label() } } View code
line 50

Hi Duncan, a bug in intellij prevents me from offering a proper fix, however I’ll try to write the fix blindly and please try it out. In the MyUI file, add this at line 34:

private lateinit var welcomeMenuButton: MenuButton

Then, change line 50 as follows:

welcomeMenuButton = menuButton("Welcome", VaadinIcons.MENU, "3", WelcomeView::class.java)

Now we can reference that button and change the count from the background thread. Just add the following code into ui.access at line 73:

welcomeMenuButton.badge = "45" Please let me know if this worked for you :wink:

It is important that you change the state of the Vaadin components only with the Vaadin UI lock held: either from Vaadin listeners, or from UI.init(), or by using ui.access {} block. Otherwise, you will not see the effect from your changes, and silent internal corruption caused by multithreaded concurrent modification may occur, which may cause random bugs. So please be careful when modifying Vaadin components from a background thread. Just remember that from your thread you need to call ui.access {} and modify Vaadin components in that access block and you will be safe.

Thank you very much Martin, it has worked!

Below is a snippet of the ui.access{}

ui.access{
    welcomeMenuButton.badge = "$timer"
}