Home > OutlineView > Recipe 11: Why my OutlineView is not refreshed?

Recipe 11: Why my OutlineView is not refreshed?

Problem

Your outline view needs to be updated from data that are received from the network or when you edit a cell and press ENTER, nothing happens. Or when you add/remove a row the outline view is not updated.

Solution

For the cells to be updated/refreshed, one needs to add a property change listener to the underlying bean that when a property is changed, the node is refreshed, e.g.

public class MyNode extends BeanNode<MyBean> {

   private final transient PropertyChangeListener pcl = new PropertyChangeListener() {

     @Override
     public void propertyChange(final PropertyChangeEvent evt) {
       firePropertySetsChange(null, getPropertySets());
     }
   };

   public MyNode(MyBean bean) throws IntrospectionException {
     super(bean, Children.LEAF, Lookups.singleton(bean));
     bean.addPropertyChangeListener(pcl);
   }
}

and you need to fire a property change in your bean when a property changed.
This will refresh your outline view each time a cell value is changed.
To refresh your outline view when a row is added/removed, you need to modify your ChildFactory accordingly, e.g.

public class MyChildFactory extends ChildFactory<MyBean> {

   private final MyProvider provider;
   private final transient PropertyChangeListener pcl = new PropertyChangeListener() {

     @Override
     public void propertyChange(final PropertyChangeEvent evt) {
       refresh(true);
     }
   };

   public MyChildFactory() {
     provider = Lookup.getDefault().lookup(MyProvider.class);
     provider.addPropertyChangeListener(pcl);
   }

   @Override
   protected boolean createKeys(final List<MyBean> toPopulate) {
     toPopulate.addAll(provider.list());
     return true;
   }
}

where MyProvider is a service provider that popuplates your ChildFactory. Again, you need to fire a property change in your provider when an instance of MyBean is added/removed from it.

Advertisements
Categories: OutlineView
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: