Refresh label in combination with UriFragmentChangedListener

I created a custom component that should represent a breadcrumb, which is based on a regular label.
The code works as expected, but the label value is not updated on the screen.
The value of the label is set correctly, because when debugging I can see the previously set text.

How can I make the label refresh on screen ?


public class BreadCrumbs extends Label implements Page.UriFragmentChangedListener {

    private static final String BREAD_CRUMB_START_ELEMENT = "Start";

    public BreadCrumbs() {
        super(BREAD_CRUMB_START_ELEMENT);
    }

    /**
     * The character used to separate parts from the path. Doesn't need to be /.
     */
    private static final String FRAGMENT_SEPARATOR = "/";

    /**
     * Splits the URI Fragment into its composite parts, using {@link #FRAGMENT_SEPARATOR} as its delimiter.
     * @return
     */
    private String[] splitFragmentIntoPaths(String cleanFragment) {
        return cleanFragment.split(FRAGMENT_SEPARATOR);
    }

    /**
     * Get the current fragment without superfluous {@link #FRAGMENT_SEPARATOR}
     * @return A cleaned URI fragment.
     */
    private String getCleanFragment(Page.UriFragmentChangedEvent event) {
        String fragment = event.getUriFragment();
        if (fragment != null && fragment.length() > 0) {
            fragment = fragment.replaceFirst("^" + FRAGMENT_SEPARATOR + "+", "");
            fragment = fragment.replaceFirst(FRAGMENT_SEPARATOR + "+$", "");
            return fragment;
        } else {
            return "";
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void uriFragmentChanged(Page.UriFragmentChangedEvent event) {
        String cleanFragment = getCleanFragment(event);
        String []cleanFragments = splitFragmentIntoPaths(cleanFragment);

        StringBuilder breadCrumbTextBuilder = new StringBuilder();
        breadCrumbTextBuilder.append(BREAD_CRUMB_START_ELEMENT);
        for (String fragment : cleanFragments) {
            breadCrumbTextBuilder.append(" > ");
            breadCrumbTextBuilder.append(fragment);
        }

        setValue(breadCrumbTextBuilder.toString());
        markAsDirty();
    }

}

I configured this component within my UI like this :


// Link the breadcrumbs to this application
getPage().addUriFragmentChangedListener(breadCrumbs);

I’m guessing that the value of the label is updated on the server side but the change is not yet send to the client. Try making a server request (e.g. by clicking a button or using polling or push) after you set the value.

I know there should indeed be a request in order to update component, but there is in fact a request to the server because an UriFragementChangeEvent is triggered.

If I click one of the buttons on the page or even attach a clicklistener on the surrounding layout this component is not refreshed or redrawn.

Hi. Try using the
setCaption()
method instead of
setValue()
. Also, consider using composition instead of extension for the
Label
in your component.

I found the issue when adding a clickhandler and changed the value there.
The problem is I have two different instances of my component, probably made a mistake because I’m using Spring to inject my components in different views.