[sample] tableview + arrayContentProvide + custom labelprovide

2215 ワード

[sample] tableview + arrayContentProvide + custom labelprovide
public class TableViewer3 {
    static String[] columnProperties = {"col1","col2","col3"};
    
    static Display display = Display.getDefault();

    public static void creatShellArae(Shell shell) {
        Table table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
        table.setHeaderVisible(true);
        for(int i=0; i < 3; i++) {
            TableColumn column = new TableColumn(table, SWT.NONE);
            column.setText("column " + i); column.setWidth(100);
        }

        TableViewer viewer = new TableViewer(table);
        viewer.setContentProvider(new ArrayContentProvider());
        
        viewer.setColumnProperties(columnProperties);
        viewer.setLabelProvider(new ITableLabelProvider() {
            public void addListener(ILabelProviderListener listener) {}
            public void dispose() {}
            public boolean isLabelProperty(Object element, String property) {
                return false;
            }
            public void removeListener(ILabelProviderListener listener) {}
            public Image getColumnImage(Object element, int columnIndex) {
                return null;
            }
            public String getColumnText(Object element, int columnIndex) {
                return element.toString() + " " + columnIndex;
            }
        });

        String[] input = {"a","b","c"};
        viewer.setInput(input);

    }

    public static void main(String[] args) {
        Shell shell = new Shell(display);
        shell.setSize(500, 300);
        shell.setLayout(new FillLayout());

        creatShellArae(shell);

        shell.open();
        shell.layout();
        
        while (!shell.isDisposed())
            if (!display.readAndDispatch())
                display.sleep();

    }

}