TextArea component is not updated and is not edited

17 views
Skip to first unread message

Rubén V

unread,
Sep 27, 2020, 3:51:36 PM9/27/20
to CodenameOne Discussions
Hi,

I have a list that is composed of a set of containers as shown in the figure. As you can see, there are 2 buttons that increase or decrease the quantity field by one unit (yellow circle). Each time I press the buttons the total amount updates smoothly, but the amount remains the same. Additionally, I need the quantity field to be able to be modified by the user without using the buttons.

I appreciate the support

My code:


    private Container creaItemInventario(VehiculoInventario iNv) {

        TextArea txCantidad = new TextArea(iNv.cantidadPedido.get() != null ? iNv.cantidadPedido.get().toString() : "0");
        txCantidad.setUIID("MultiLine3");

        TextArea txPrecio = new TextArea(redondear(iNv.precio.get(), 2));
        txPrecio.setUIID("MultiLine3");
        txPrecio.setEditable(false);

        TextArea txMonto = new TextArea();
        txMonto.setUIID("LabelPupleTitle");
        txPrecio.setEditable(false);

        MultiButton mNombreArticulo = new MultiButton();
        mNombreArticulo.setTextLine1(iNv.descripcionArticulo.get());
        mNombreArticulo.setUIIDLine1("MultiLine2");
        mNombreArticulo.setTextLine2(iNv.codigo.get() + " (" + iNv.tamano.get() + ")");
        mNombreArticulo.getAllStyles().setBgTransparency(150);
        mNombreArticulo.setHorizontalLayout(false);

        calculoMonto(txCantidad, txPrecio, txMonto, 0d);

        Container cnPrecio = new Container(new BoxLayout(BoxLayout.X_AXIS)).add(usr.simboloMoneda.get()).add(txPrecio.getText());

        BorderLayout border = new BorderLayout();
        border.setCenterBehavior(BorderLayout.CENTER_BEHAVIOR_CENTER_ABSOLUTE);
        Container cnMonto = new Container(border).add(BorderLayout.CENTER, txMonto);
        cnMonto.setUIID("LabelPurpleTitle");

        Button btMenos = new Button(FontImage.createMaterial(FontImage.MATERIAL_REMOVE, UIManager.getInstance().getComponentStyle("Button")));
        btMenos.addActionListener((e) -> {
            if (Double.parseDouble(txCantidad.getText()) > 1) {
                calculoMonto(txCantidad, txPrecio, txMonto, -1d);
            }
        });
        Button btMas = new Button(FontImage.createMaterial(FontImage.MATERIAL_ADD, UIManager.getInstance().getComponentStyle("Button")));
        btMas.addActionListener((e) -> {
            calculoMonto(txCantidad, txPrecio, txMonto, +1d);
        });

        Style s = UIManager.getInstance().getComponentStyle("Menu");

        BorderLayout border1 = new BorderLayout();
        border1.setCenterBehavior(BorderLayout.CENTER_BEHAVIOR_CENTER_ABSOLUTE);

        s = UIManager.getInstance().getComponentStyle("ButtonRed");
        Button btEliminar = new Button(FontImage.createMaterial(FontImage.MATERIAL_DELETE, s, 8));
        btEliminar.setUIID("ButtonTransparent");

        Label lbImagenInventario = new Label();
        FontImage.setMaterialIcon(lbImagenInventario, FontImage.MATERIAL_LOCAL_SHIPPING, 7);
        Label lbInventario = new Label(Double.toString(iNv.cantidad.get()));
        lbInventario.setUIID("MensajeRojo");
        FloatingActionButton badgeInventario = FloatingActionButton.createBadge("");
        badgeInventario.setText(lbInventario.getText());
        badgeInventario.setUIID("MensajeRojo");

        Label lbImagenInventarioEmpresa = new Label();
        FontImage.setMaterialIcon(lbImagenInventarioEmpresa, FontImage.MATERIAL_HOME_WORK, 7);
        Label lbInventarioEmpresa = new Label(Double.toString(iNv.cantidadInventario.get()));
        FloatingActionButton badgeInventarioEmpresa = FloatingActionButton.createBadge("");
        badgeInventarioEmpresa.setText(lbInventarioEmpresa.getText());
        badgeInventarioEmpresa.setUIID("MensajeRojo");

        Container cnInventario = new Container(new GridLayout(1, 2));
        cnInventario.add(badgeInventario.bindFabToContainer(lbImagenInventario, Component.RIGHT, Component.CENTER));
        cnInventario.add(badgeInventarioEmpresa.bindFabToContainer(lbImagenInventarioEmpresa, Component.RIGHT, Component.CENTER));

        Container cnCantidad = new Container(new GridLayout(1, 3)).add(btMenos).add(txCantidad.getText()).add(btMas);
        Container cnCentro = new Container(new BoxLayout(BoxLayout.Y_AXIS)).add(cnPrecio).add(cnCantidad);

        Container cnItem = new Container(new BorderLayout());
        cnItem.add(BorderLayout.NORTH, mNombreArticulo);
        cnItem.add(BorderLayout.EAST, cnMonto);
        cnItem.add(BorderLayout.WEST, cnCentro);
        cnItem.add(BorderLayout.SOUTH, cnInventario);

        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(1, 1, 1, 1);

        cnItem.putClientProperty("busqueda", iNv.descripcionArticulo.get());
        cnItem.putClientProperty("registro", iNv);

        mNombreArticulo.addActionListener((e) -> {
            cnItem.revalidate();
            Dialog.show("Mensaje", "Pendiente", "continuar", null);
        });

        return cnItem;
    }

    private void calculoMonto(TextArea txCantidad, TextArea txPrecio, TextArea txMonto, Double incremento) {
        txCantidad.setText(Double.toString(Double.parseDouble(txCantidad.getText()) + incremento));
        Double c = Double.parseDouble(txCantidad.getText());
        Float p = Float.parseFloat(txPrecio.getText());
        txMonto.setText(redondear(p * c, 2));
        this.revalidate();
    }

Rubén V

unread,
Sep 27, 2020, 3:53:14 PM9/27/20
to CodenameOne Discussions
Upps, the image does not load

Shai Almog

unread,
Sep 28, 2020, 1:44:35 AM9/28/20
to CodenameOne Discussions
You can use an action listener or use a TextField with a data change listener to track changes to the text component. This will let you recalculate based on user input assuming this is valid input (notice you need to catch NumberFormatException that can be thrown by the parseDouble method).

I'm not sure why the number isn't updated visually I'm not sure which field this maps to and how that's represented in the UI from this code.

Rubén V

unread,
Sep 28, 2020, 1:36:44 PM9/28/20
to CodenameOne Discussions
Hi,

Add the listener and it doesn't work because even though the field is defined as modifiable, it doesn't allow any changes. An interesting point is that the txMonto field can be modified.

I also tried putting the txQuantity field in another separate container and it is not modifiable yet. These pretty confusing.

What could be the problem?

Rubén V

unread,
Sep 28, 2020, 5:08:43 PM9/28/20
to CodenameOne Discussions
Hi,

The problem was the way he referenced the field txCantidad

It does not work:
Container cnCantidad = new Container(new GridLayout(1, 3)).add(btMenos).add(txCantidad.getText()).add(btMas);

It works:
Container cnCantidad = new Container(new GridLayout(1, 3)).add(btMenos).add(txCantidad).add(btMas);

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