我創建了一個TableViewer
表格以顯示來自ArrayList
的數據。每次向列表中添加新項目時,我都想刷新表格。但是現在表格會一直等到我的所有數據都被添加到列表中,那麼表格將一次顯示所有數據。有人能幫我解決這個問題嗎?我被困在這裏很長一段時間......下面是一些代碼每次將新項目添加到列表時,刷新TableViewer表格
private void buildPerformanceTable(
IPerformanceDataRetriever performanceDataRetriever) {
tableViewer.setContentProvider(new JobProfileContentProvider());
tableViewer.setLabelProvider(new JobProfileLabelProvider());
tableViewer.setComparator(new JobProfileViewerComparator());
Table table = tableViewer.getTable();
for (ColumnType columnType : ColumnType.values()) {
buildTableColumn(columnType.getColumnName(),
columnType.getColumnIndex());
}
tableViewer.setInput(performanceDataRetriever);
for (int i = 0; i < table.getColumnCount(); i++) {
table.getColumn(i).pack();
}
table.setHeaderVisible(true);
table.setLinesVisible(true);
}
public class JobProfileContentProvider implements IStructuredContentProvider{
private static final long SERIAL_VERSION_UID = 6452458171326245659L;
/**
* {@inheritDoc}
*/
@Override
public Object[] getElements(Object object) {
return ((IPerformanceDataRetriever) object).providePerformanceData()
.toArray();
}
/**
* <b>This method is not implemented.</b> <br>
*
* {@inheritDoc}
*/
@Override
public void dispose() {
}
/**
* <b>This method is not implemented.</b> <br>
*
* {@inheritDoc}
*/
@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}
public class JobProfileInfoMock implements IPerformanceDataRetriever {
/**
* Creates an instance of list containing active JobProfile.
*
* @return List contains JobProfile.
*/
public static List<JobProfile> getJobProfileWithAllActiveJobs(){
List<JobProfile> JobProfiles = new ArrayList<JobProfile>();
for(int i=1;i<=6;i++){
JobProfile profile = new JobProfile.ProfileBuilder().jobId(i)
.cpuUsage(80+i).memoryUsage(40+i).ipAddress("192.1.12.4"+i).isActive(true)
.build();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
JobProfiles.add(profile);
System.out.println(JobProfiles.size());
}
return JobProfiles;
}
}
感謝。我已經解決了這個問題。 – RayHong 2014-10-02 14:09:35