Search code examples
codenameone

How to add conditionally a `Button` to a `Toolbar` `Form` `Component`?


I have created a Form that is set to function as the Toolbar Component of a mobile application that I am working on. I want to add a Button to it conditionally, depending on which Form screen is the Toolbar being added to. I have tried the following but does not work: if(aForm.getTitle().equals("Target Form Screen's Title")) aForm is the Form to which the Toolbar is currently being added to. It is supposed to trigger the conditional when the Form title matches the target Form screen.


Solution

  • You can compare to the title in this way and it could work but generally it's a good practice to separate the UI and specific strings from the state. So you should have a separate class that controls the application state and decides what should be added.

    If you need some state within the UI you can use putClientProperty and getClientProperty.