求助,求助!!关于tableviewer的
以下是我的源程序.我的tableviewer里总是显示不了数据啊!
-------wodeceshichengxu.java---------
package temp;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import java.util.List;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.ColumnWeightData;
public class wodeceshichengxu {
static class TableLabelProvider extends LabelProvider implements ITableLabelProvider {
public String getColumnText(Object element, int columnIndex) {
dataEntity o = (dataEntity)element; //类型转换
if(columnIndex==0) return o.getFileName();
if(columnIndex==1) return o.getModifiedDate().toString();
return " ";
//return element.toString();
}
public Image getColumnImage(Object element, int columnIndex) {
return null;
}
public void addListener(ILabelProviderListener listener) {}
public void dispose() {}
public boolean isLabelProperty(Object element, String property) {
return false;
}
public void removeListener(ILabelProviderListener listener) {}
}
static class ContentProvider implements IStructuredContentProvider {
public Object[] getElements(Object inputElement) {
if(inputElement instanceof List) //加一个类型判断
return ((List)inputElement).toArray();
else
return new Object[0];
}
public void dispose() {
}
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
}
private static Table table;
/**
* Launch the application
* @param args
*/
public static void main(String[] args) {
final Display display = Display.getDefault();
final Shell shell = new Shell();
shell.setSize(500, 375);
shell.setText( "SWT Application ");
//
shell.open();
final TableViewer tableViewer = new TableViewer(shell, SWT.BORDER);
table = tableViewer.getTable();
table.setBounds(64, 71, 100, 100);
table.setHeaderVisible(true); //表头显示
table.setLinesVisible(true); //表格线显示
TableLayout layout = new TableLayout();
layout.addColumnData(new ColumnWeightData(40));
new TableColumn(table, SWT.NONE).setText( "文件名 ");
layout.addColumnData(new ColumnWeightData(40));
new TableColumn(table, SWT.NONE).setText( "最后修改时间 ");
tableViewer.setLabelProvider(new TableLabelProvider());
tableViewer.setContentProvider(new ContentProvider());
//tableViewer.setInput(new Object());
tableViewer.setInput(dataFactory.getData());
//tableViewer_2.setLabelProvider(new TableLabelProvider());
//tableViewer_2.setContentProvider(new ContentProvider());
//tableViewer_2.setInput(new Object());
// tableViewer_2.setInput(dataFactory.getData());
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
}
------------dataEntity.java----------------------------
package temp;
//import java.util.Date;
public class dataEntity{
private String fileName;
private String modifiedDate;
public String getFileName(){return fileName;}
public void setFileName(String fileName){this.fileName = fileName;}
public String getModifiedDate(){return modifiedDate;}
public void setModifiedDate(String modifiedDate){this.modifiedDate = modifiedDate;}
}
------------------------dataFactory.java------------
package temp;
import java.util.List;
import java.util.ArrayList;
//import java.util.Date;
public class dataFactory{
public static List getData(){
List list = new ArrayList();
{
dataEntity dE = new dataEntity();
dE.setFileName( "file 1 ");
dE.setModifiedDate( "haha ");
System.out.println( " "+dE.getModifiedDate());
System.out.println(dE.getFileName());
list.add(dE);
}
{
dataEntity dE1 = new dataEntity();
dE1.setFileName( "file 2 ");
dE1.setModifiedDate( "hoho ");
list.add(dE1);
}
return list;
}
}
[解决办法]
layout.addColumnData(new ColumnWeightData(40));
new TableColumn(table, SWT.NONE).setText( "文件名 ");
layout.addColumnData(new ColumnWeightData(40));
new TableColumn(table, SWT.NONE).setText( "最后修改时间 ");
错在这里,应该在标签器中定义列内容,你在界面中定义了,所以不显示。