Formatting button on

Hello,
I am working with a table, there are files within it that can be validated with a toggle on or off button, but in the case that it’s duplicated I want there to be a button to delete the file. Up to this point, the mechanism works. But when I delete one of the duplicate files and it should display the toggle instead of the delete button, I can not get it to refresh the button back to a toggle switch.

image

the code is setup something like this:
Column Generator and called function:

 @Install(to = "psaArchivoPublicoesTable.validado", subject = "columnGenerator")
private Component psaArchivoPublicoesTableValidadoColumnGenerator(PsaArchivoPublico psaArchivoPublico) {

    if (security.isSpecificPermitted("psaArchivoPublico.validar")) {

        return createSwitchValidar(psaArchivoPublico);

    } else {
        return createCheckBoxValidado(psaArchivoPublico);
    }
}

Button / Switch

  private Component createSwitchValidar(PsaArchivoPublico psaArchivoPublico) {
    Button button = uiComponents.create(Button.class);
    UUID archivoTipoPublicoID = psaArchivoPublico.getPsaArchivoTipo().getId();

    PsaArchivoTipo psaArchivoTipoInvalido = archivosPsaTipoRequeridos.stream()
            .filter(psaArchivoTipo -> psaArchivoTipo.getId()
                    .equals(archivoTipoPublicoID)).findAny().orElse(null);

    if (psaArchivoTipoInvalido != null) {
        button.addStyleName("danger");
        button.setIcon(icons.get(CubaIcon.TRASH));
        button.setAction(new BaseAction("enabled")
                .withPrimary(true).withHandler(event -> {
                    try {
                        psaArchivoService.delete(psaArchivoPublico);
                        psaArchivoPublicoesDl.load();
                        psaArchivoPublicoesTable.repaint();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    psaArchivoPublicoesDl.load();
                }));
        return button;
    }

    if (psaArchivoPublico.getValidado() == null || psaArchivoPublico.getValidado() == false) {
        button.setIcon(icons.get(CubaIcon.TOGGLE_OFF));
        button.addStyleName("quiet");
    } else {
        button.setIcon(icons.get(CubaIcon.TOGGLE_ON));
        button.addStyleName("secondary");
    }
    button.setAction(new BaseAction("enabled")
            .withPrimary(true).withHandler(event -> {
                psaArchivoService.validar(psaArchivoPublico);
                psaArchivoPublicoesDl.load();
            }));
    return button;

Hello @arturoams,

I am enclosing a simple example where a Button or a Label is generated depending on the value of a isButton boolean variable. When the button is pressed, the value of the isButton variable changes and when the Table#repaint() method is called, a Label is generated instead of the Button.

public class OrderBrowse extends StandardLookup<Order> {

    @Inject
    protected UiComponents uiComponents;
    @Inject
    protected GroupTable<Order> ordersTable;

    protected boolean isButton = true;

    @Install(to = "ordersTable.price", subject = "columnGenerator")
    protected Component ordersTablePriceColumnGenerator(Order order) {
        if (isButton) {
            Button button = uiComponents.create(Button.NAME);
            button.addStyleName(WebButton.PRIMARY_ACTION_STYLENAME);
            button.setCaption(String.valueOf(order.getPrice()));
            button.setAlignment(Component.Alignment.MIDDLE_CENTER);
            button.addClickListener(clickEvent -> {
                isButton = false;
                ordersTable.repaint();
            });
            return button;
        } else {
            Label<Integer> label = uiComponents.create(Label.TYPE_INTEGER);
            label.setValue(order.getPrice());
            return label;
        }
    }
}

Based on the code you attached, you need to check if the value of the psaArchivoTipoInvalido variable changes. And in order to update the component in the generator, call the Table#repaint() method.

Regards,
Gleb