Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to reload the whole form after certain action on button Codenameone

Tags:

codenameone

I want to refresh my form when I remove a product from my cart I tried everything but didn't work is there a way to do it?

There is my Cart class which displays the products in my cart and where I want to when I remove a product from my cart I want to refresh the whole form but didn't know how to do it I barely tried every method but still can't be done

public class Cart {
  Form f;

  public Cart() throws IOException {
      f = new Form("cart",BoxLayout.y());

      Button b = new Button("back");
      b.addActionListener(e->{
         AfficherProduits sp;
          try {
              sp = new AfficherProduits();
              sp.getF().show();
          } catch (IOException ex) {
              System.out.println("ERREUR DANS RETOUR LISTE PRODUITS ");
          }
      });
      f.add(b);

      //**********************************instanciation du panier********************************************************
      Panier panier=Panier.getInstance();


      //********************************Parcourir le panier**************************************************************
       ComponentGroup cg = new ComponentGroup();
      for (Lignedecommande c : panier.p)
      {
          Container c4 =new Container(BoxLayout.x());
          Container c3 =new Container(BoxLayout.y());
          Container c2=new Container(BoxLayout.x());
          Container c1=new Container(BoxLayout.y());
          Container c5=new Container(BoxLayout.y());

      //***************************les elements du containers************************************************************
          ImageViewer iv=new ImageViewer();
          iv.setImage(Image.createImage("/"+ c.getProduct().getImage()).scaled(80, 80));

          Button bt=new Button("X");

          bt.addActionListener(e->{
             panier.removeLine(c);
             //ShowListProduct sp=new ShowListProduct();
             //sp.getF().show();
             f.removeComponent(cg);
             f.refreshTheme();
          });
          //********************les boutons de modif quantite******************************************
          Button b1=new Button("+");
          Button b2=new Button("-");
          bt.getStyle().setPadding(0,0,0,0);

          //*****************************mettre le bouton X au milieu****************************************************
          Label lb1=new Label(".");
          Label lb2=new Label(".");
          lb1.setVisible(false);
          lb2.setVisible(false);
          c1.add(lb1);
          c1.add(bt);
          c1.add(lb2);

          c5.add(b1);
          c5.add(b2);

          c4.add(c1);
          c4.add(iv);

          c2.add(new Label(Integer.toString(c.getQuantite())));
          c2.add(c5);
          //c2.add(b2);

          //c3.add(new SpanLabel(c.getProduct().getName()));
          c3.add(c2);


          c4.add(c3);
          c4.add(new Label("$"+Float.toString(c.getProduct().getPrix()*c.getQuantite())));

          cg.add(c4);
      }
      f.add(cg);
  }

  public Form getF() {
      return f;
  }

  public void setF(Form f) {
      this.f = f;
  }
}

and there is how I call this page to see my cart

  h = new AfficherProduits();
  h.getF().show();
like image 896
AOUADI Slim Avatar asked Dec 06 '25 04:12

AOUADI Slim


1 Answers

Don't reload. Recreate and show.

Move all the code from the constructorto a method called createCartForm(). The constructor would then do:

f = createCartForm();

A reload would just become:

setF(h.createCartForm());
getCurrentForm().setTransitionOut(CommonTransitions.createEmpty());
h.getF().show();

I replace the form with a new instance and remove the default transition out so the replacement won't be animated.

like image 200
Shai Almog Avatar answered Dec 08 '25 00:12

Shai Almog



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!