Forcing repaint - good or bad?

Hi,

I faced a minor issue when the text field values did not refresh without forcing repaint. Shouldn’t the view refresh automatically whenever the model changes? For me it feels like something that should not ever be required, or am I wrong? However, for some reason those methods exist… anyway:

I used bean item adapter

BeanItem adapter = new BeanItem(workPlaceBean);

And when user ID changes from a comboBox, the following listener is called:

private Property.ValueChangeListener userChangeListener = new Property.ValueChangeListener() {

    @Override
    public void valueChange(ValueChangeEvent event) {
           
            String userId = event.getProperty().toString();

            parentForm.getWorkPlaceBean().setFirstName("FN");
            parentForm.getWorkPlaceBean().setLastName("LN");
            parentForm.getWorkPlaceBean().setCostCenter("CC");
            parentForm.getWorkPlaceBean().setUserId(userId);
            parentForm.getWorkPlaceBean().setPersonalNumber("PN");

           // AFTER ADDING THIS FORCING, THE VIEW ALSO CHANGED 

            nameField.requestRepaint();
            personalField.requestRepaint();
            costField.requestRepaint();

    }
};

and as the comment says, there is the forcing part. On the second time it refreshed without forcing…

  • L -

The view does not have any chance to be notified about invocation of your own setter methods, as you do not notify any listeners about those changes from those setters, I believe.

In order to give the components a chance to be notified you must use:

parentForm.getItemProperty("firstName").setValue("FN");
...

Basically every Field (including those in Forms) uses Property as its data source. If given Property instance implements ValueChangeNotifier interface, the Field registers its ValueChangeListener to be notified about the value change, thus being able to change its value accordingly. All such listeners are notified when Property.setValue method is invoked.

You use BeanItem, and Property objects of such items do implement ValueChangeNotifier interface. The only thing is, you must use Property.setValue for listeners to be notified.

Hi,

If the
Item
is modified the view is automatically refreshed, but unfortunately the
BeanItem
can not know automatically when the underlying bean (pojo) changes. This is a java issue - you can’t listen to changes on plain objects (without using e.g. beansbinding).

It’s “cleaner” to arrange so that you never have to refresh anything specifically; this generally means you have to update the model using the Vaadin specific interfaces, not directly changing the pojo (in this particular case, get the Items Properties and set the value of those).

The requestRepaint() methods are mainly used by the component itself, to notify the framework that it’s “dirty”, but you can call it yourself when needed, and sometimes it’s needed (or just handy). However, if you arrange so that you don’t have to refresh explicitly, you’re less likely to forget a refresh somewhere or (more likely) trigger a refresh when it’s not needed.

Best Regards,
Marc


(edit: Michal Kuleta said it well above while I was typing :slight_smile:

Thanks Michal and Marc.

  • L -