How to change the state of a record that is in the main form, depending on what is done in the secondary form

23 views
Skip to first unread message

Rubén V

unread,
Dec 21, 2020, 10:26:32 PM12/21/20
to CodenameOne Discussions
Hi,

Forgive my audacity, but this time my question is about logic.

I have two forms (classes) that interact with each other. The main one has a set of containers that in each item has a status label (starts pending) and a "multibutton" component that triggers the second form.
Depending on what you do in the second form the status of the item in the first form should change (for example to completed).

In the main form create a public method that is in charge of changing the status of the item.


When I fire the second form, I include in the constructor the object that represents the first form and in this way I can execute the public method as I please.

public class mainForm extends Form {

   private container cnItem = new Container();

   public mainForm() {

      MultiButton mbMyButton;
      mbMyButton.addActionListener ((e) -> {
          setItemSelect(cn);
          new secondForm (this, md) .show ();
      }
  }
  
   public void changeStatus () {
      changeItemStatus(getItemSelect());
   }

   private Container getItemSelect() {
       return cnItem;


   private void setItemSelect(Container cn) {
       cnItem = cn;
   }

}

public class secondForm extends Form {

   public secondForm (MainForm mf, ModelDato md) {
      if (md.condition) {
         mf.changeStatus ();
      }
}

This works for me, but I have always thought that there must be a more orderly way. 

I thank you if you can point me to a better form of logic.

Thanks.

P5music

unread,
Dec 22, 2020, 5:54:14 AM12/22/20
to CodenameOne Discussions
Dialogs have a useful method that returns a Command,  showDialog(). By that I was able to avoid things like what you are saying, and I can also return parameters inside the command with the setProperty() method. No runnables needed and the code is readable.
Dialogs have issues in certain cases, unfortunately. Someone advices to use the InteractionDialog class, that is bad IMHO because of the necessary handling of the mouse events and the back command.

But I was said that Dialogs are just like Forms.

So you could ask them to create a method similar to showDialog(),
like 
public Command showBackWithCommand()
{}

Regards

Shai Almog

unread,
Dec 22, 2020, 9:11:31 PM12/22/20
to CodenameOne Discussions
Hi,
We generally create a model package for most of our projects where business logic is maintained. This also includes the current state of the application with code that has no UI logic in it. For most apps we see the model as something global that we don't need to pass around to each object. We use a singleton to manage access to that model logic which is pretty convenient.

Rubén V

unread,
Dec 23, 2020, 8:24:27 PM12/23/20
to CodenameOne Discussions
En español:  "muchas gracias"
Reply all
Reply to author
Forward
0 new messages