Did You Know: JFace ArrayContentProvider

When coding in Eclipse RCP or, for that matter, any JFace based UI, you often times are implementing viewers, which require a content provider and a label provider. In most cases, you’re dealing with the IStructuredContentProvider API, which would be appropriate for both the ListViewer and TableViewer classes.

If you have a need to populate these views with data that is effectively static, you can use the org.eclipse.jface.viewers.ArrayContentProvider class to get you up and running quickly; and it is really quite functional in many cases.The array content provider is simply a basic implementation of the IStructuredContentProvider API that expects the input on the viewer to either be an array or a collection - the items returned by the content provider to fulfill the IStructuredContentProvider are the items in that array.

Note that the Javadoc of this class says this:

This implementation of IStructuredContentProvider handles the case where the viewer input is an unchanging array or collection of elements.

That is technically true, however you can force it to re-consider the contents, simply by calling setInput(...) again on the viewer.

Here is some example code:

TableViewer viewer = //..
viewer.setContentProvider(new ArrayContentProvider());
viewer.setLabelProvider(new MyLabelProvider());
viewer.setInput(new Object[] { "String 1", "String 2", "String 3" });
 
// ...
 
// Causes the UI to re-render with String 4, 5, and 6 
viewer.setInput(new Object[] { "String 4", "String 5", "String 6" });