SWT&JFace

    protected Control createContents(Composite parent) {
        Composite container = new Composite(parent, SWT.NONE);
        container.setLayout(new GridLayout(2, false));

        TableColumnLayout layout=new TableColumnLayout();
        Composite composite = new Composite(container, SWT.NONE);
        composite.setLayoutData(new GridData(SWT.LEFT, SWT.FILL, false, true, 1, 1));
        composite.setLayout(layout);

        tableViewer = new TableViewer(composite, SWT.BORDER | SWT.FULL_SELECTION);
        table = tableViewer.getTable();
        table.setHeaderVisible(true);
        table.setLinesVisible(true);
        CellEditor[] editors = new CellEditor[2];
        String [] columnProperties = new String[2];
        for(int i=0;i<2;i++){
//            TableViewerColumn tableViewerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
//            TableColumn tblclmnFirst = tableViewerColumn.getColumn();
            TableColumn tblclmnFirst = new TableColumn(table,SWT.NONE);
           layout.setColumnData(tblclmnFirst, new ColumnWeightData(2, ColumnWeightData.MINIMUM_WIDTH, true));
             tblclmnFirst.setText("A"+i);
            tblclmnFirst.setWidth(50);
            editors[i] = new TextCellEditor(table);
            columnProperties[i] = ""+i;
        }
        tableViewer.setContentProvider(new MatrixContentProvider());
        tableViewer.setLabelProvider(new MatrixLabelProvider());
        tableViewer.setInput(matrixA);
       
        tableViewer.setColumnProperties(columnProperties);
        tableViewer.setCellEditors(editors);
        tableViewer.setCellModifier(new ICellModifier(){

            @Override
            public boolean canModify(Object element, String property) {
                return true;
            }

            @Override
            public Object getValue(Object element, String property) {
                double[] d = (double[])element;
                System.out.println("ICellModifier:: "+Arrays.toString(d)+",property="+property);
                int i = Integer.parseInt(property);
                return String.valueOf(d[i]);
            }

            @Override
            public void modify(Object element, String property, Object value) {
                TableItem item = (TableItem)element;
                double[] d = (double[])item.getData();
               
                int i = Integer.parseInt(property);
                try{
                    d[i]=Double.parseDouble(String.valueOf(value));                       
                }catch(Exception e){
                    e.printStackTrace();
                }
                System.out.println("ICellModifier::---modify--- "+Arrays.toString(d)+",property="+property+",value =" +value);
                outMatrixA();
                tableViewer.refresh();
            }
        });

        addButton = new Button(container, SWT.NULL);
        addButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                double [] d=new double[2];
                matrixA.add(d);
                tableViewer.add(d);
            }
        });
        addButton.setText("添加");

public class MatrixLabelProvider implements ITableLabelProvider {
    public String getColumnText(Object data, int index) {
        double [] d = (double[])data;
        return String.valueOf(d[index]);
    }

}

public class MatrixContentProvider implements IStructuredContentProvider {
    public Object[] getElements(Object list) {
        if(list instanceof List){
            return ((List)list).toArray();
        }
        return null;
    }

}

你可能感兴趣的:(jface)