Dialog component does not stop

77 views
Skip to first unread message

Rubén V

unread,
Jan 21, 2021, 5:39:19 PM1/21/21
to CodenameOne Discussions
Hi,

From a Form I call a method that launches the Dialog component. The launch code is this:

 

mbNombre.addActionListener((e) -> {

            if (seleccion) {

                Command ok = new Command("ok");

                Visita v = new Visita();

                v.visitaId.set(null);

                v.clienteId.set(clt.clienteId.get());

                v.latitud.set(clt.latitud.get());

                v.longitud.set(clt.longitud.get());

                Dialog d = creaVisita(v, clt.nombre.get(), ok);

                if (d.showDialog() == ok) {

                    procesaRegistro(v, VISITA);

                    formaVisita.creaItemVisitas(v);

                    muestraVisitas(formaVisita.getContenedorVisitas());

                }

                fRetorno.showBack();

            }

        });

 

The method displays the Dialog compent with all the components that you add to it without problems. Pressing the btAceptar button performs 2 validations and displays the error messages in each case. The problem is that it automatically executes the "dispose" and returns control to the small code where it was launched (as if it were pressing the "ok" component).

 

    private Dialog creaVisita(Visita v, String nombre, Command ok) {

 

        Dialog dlVisita = new Dialog(new BorderLayout());

        Label lbTitulo = new Label(nombre, "MultiLine1");

        lbTitulo.getAllStyles().setAlignment(Component.CENTER);

        dlVisita.setTitleComponent(lbTitulo);

        dlVisita.setDialogUIID("Form");

 

        Label lbDescripcion = new Label(idioma.getDescripcion() + ":");

        TextArea txDescripcion = new TextArea();

        txDescripcion.setFocus(true);

        Container cnDescripcion = new Container(new BoxLayout(BoxLayout.Y_AXIS)).add(lbDescripcion).add(txDescripcion);

 

        Tarea t = new Tarea();

        ArrayList aTarea = consultaTablas(t, TAREA);

        if (aTarea.isEmpty()) {

            Dialog.show(idioma.getError(), "Inexplicable: No existen tareas creadas", idioma.getContinuar(), null);

            dlVisita.dispose();

        }

 

        Label lbTarea = new Label(idioma.getMensaje12() + ":");

        RadioButton[] rb = new RadioButton[aTarea.size() + 1];

 

        Container cnTarea = new Container(new BoxLayout(BoxLayout.Y_AXIS));

        cnTarea.setScrollableY(true);

        for (int i = 0; i < aTarea.size(); i++) {

            t = (Tarea) aTarea.get(i);

            rb[i] = new RadioButton(t.descripcion.get());

            rb[i].getAllStyles().setAlignment(Component.LEFT);

//            rb[i].setUIID("Label");

            cnTarea.add(rb[i]);

        }

 

        Container cnTareas = new Container(new BoxLayout(BoxLayout.Y_AXIS)).add(lbTarea).add(cnTarea);

 

        Container cnItem = new Container(new BorderLayout());

        cnItem.add(BorderLayout.NORTH, cnDescripcion);

        cnItem.add(BorderLayout.CENTER, cnTareas);;

 

        Style sItem = cnItem.getUnselectedStyle();

        sItem.setBgTransparency(255);

        sItem.setBgColor(0xeeeeee);

        sItem.setMarginUnit(Style.UNIT_TYPE_DIPS);

        sItem.setPaddingUnit(Style.UNIT_TYPE_DIPS);

        sItem.setMargin(1, 0, 1, 1);

        sItem.setPadding(2, 1, 2, 2);

 

        Button btCancelar = new Button(idioma.getCancelar(), "MensajeRojo");

        btCancelar.addActionListener((e) -> {

            dlVisita.dispose();

        });

 

        Button btAceptar = new Button(ok);

        btAceptar.setText(idioma.getAceptar());

        btAceptar.setUIID("MensajeVerde");

        btAceptar.addActionListener((e) -> {

            if (txDescripcion.getText().isEmpty()) {

                Dialog.show(idioma.getError(), idioma.getDescripcion() + " " + idioma.getMensaje6(), idioma.getContinuar(), null);

                return;

            }

            boolean encontro = false;

            for (int i = 0; i < rb.length - 1; i++) {

                if (rb[i].isSelected()) {

                    encontro = true;

                    break;

                }

            }

            if (!encontro) {

                Dialog.show(idioma.getError(), idioma.getTarea() + " " + idioma.getMensaje6(), idioma.getContinuar(), null);

                return;

            }

            v.descripcion.set(txDescripcion.getText());

            v.fechaFuente.set(Preferences.get("fecha", System.currentTimeMillis()));

            v.distancia.set(distancia(v.latitud.get() == null ? 0d : v.latitud.get(), v.longitud.get() == null ? 0d : v.longitud.get()));

            v.modoPendiente.set(MODO_ADICION);

            v.estado.set(PENDIENTE);

            adicionaVisita(v);

            dlVisita.dispose();

 

        });

 

        Container cnBotones = new Container(new GridLayout(1, 2)).add(btCancelar).add(btAceptar);

        dlVisita.add(BorderLayout.NORTH, cnItem);

        dlVisita.add(BorderLayout.SOUTH, cnBotones);

        return dlVisita;

    }

P5music

unread,
Jan 22, 2021, 4:30:45 AM1/22/21
to CodenameOne Discussions
What happens if you use showDialog() also in the other dialogs instead of show()?

Shai Almog

unread,
Jan 22, 2021, 5:40:26 AM1/22/21
to CodenameOne Discussions
Hi,
Use dialog.setAutoDispose(false);
Dialogs automatically dispose when a command is selected within them. This is the common use case for dialogs e.g. Yes/No, OK/Cancel etc.

Rubén V

unread,
Jan 22, 2021, 9:22:21 AM1/22/21
to CodenameOne Discussions
Hi,

Sorry, I made the change and thought it worked fine. The problem now is only when I press the btCancel button where what I had already reported happens.

Rubén V

unread,
Jan 22, 2021, 9:25:47 AM1/22/21
to CodenameOne Discussions
Hi,

It cannot be done, as I am using a "command" component to return the result of the Dialog

P5music

unread,
Jan 22, 2021, 10:38:40 AM1/22/21
to CodenameOne Discussions
Maybe 
the
  fRetorno.showBack();
has to be put inside the "ok" block and
then 
else
{
  fRetorno.showBack();
}
That instruction probably makes the other dialogs dispose as soon as they are created.

Rubén V

unread,
Jan 22, 2021, 3:21:32 PM1/22/21
to CodenameOne Discussions
Hi,

I already understand what is happening: every time I press the "btAceptar" button, the "ok" command is activated even if the validations stop the process with the "return" command. For that reason, if I subsequently press the "btCancelar" button, the dispose is executed and control returns to the starting point, assuming that the "ok" command was pressed. How do I correct this?

P5music

unread,
Jan 22, 2021, 4:03:36 PM1/22/21
to CodenameOne Discussions
I think you are using the Dialog command returning feature
together with 
your own logic.
Instead
As you say that dialog.setAutoDispose(false); is not working for you
You have not to put the ok command in the aceptar button if you want to mantain control on the disposal of the dialog. You have also to manage the cancelar.
Add ActionListeners to aceptar and cancelar, do not use commands. Manage the disposal inside the listeners.
The new dialogs that are opened in my opinion should use showDialog() to block the execution of the subsequent instructions there.
Regards

Rubén V

unread,
Jan 22, 2021, 5:42:48 PM1/22/21
to CodenameOne Discussions
Hi,

What I did was use the "Preferences" api, saving the value "1" when the "btAceptar" button was actually pressed after the validations. Once control returns to the calling routine; I validate the content of the "Preferences" and if it has "1" it proceeds to execute the rest of the instructions. Later I remove the "Preferences" to free up the memory. It's not the best way, but it works.

Thanks for support.
Reply all
Reply to author
Forward
0 new messages