除非单击可编辑单元格

时间:2019-01-22 09:57:09

标签: java swt jface e4

我目前正在使用SWT TableViewer。表有3列,第1到2列包含可编辑的文本字段数据,另一列是按钮。我将输入列表设置到tableviewer中。即使行中存在数据,数据也不可见。当我单击可编辑单元格时,数据可见。我添加了2张图片。谁能知道为什么数据不可见?

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;

class ProjectEditingSupport2 extends EditingSupport {
private int m_column;
private CellEditor m_editor;

public ProjectEditingSupport2(ColumnViewer viewer, int column) {
    super(viewer);
    // Create the correct editor based on the column index
    switch (column) {
    case 0:
        m_editor = new TextCellEditor(((TableViewer) viewer).getTable());
        break;
    case 1:
        m_editor = new TextCellEditor(((TableViewer) viewer).getTable());
        break;
    default:

    }
    m_column = column;
}

@Override
protected boolean canEdit(Object arg0) {
    // TODO Auto-generated method stub
    return true;
}

@Override
protected CellEditor getCellEditor(Object element) {
    return m_editor;
}

@Override
protected Object getValue(Object element) {
    Object value = null;
    ProjectBean2 projectBean = (ProjectBean2) element;
    switch (m_column) {
    case 0:
        value = projectBean.getProjectName();
        break;
    case 1:
        value = projectBean.getProjectLocation();
        break;
    default:

    }
    return value;
}

@Override
protected void setValue(Object element, Object value) {
    ProjectBean2 projectBean = (ProjectBean2) element;
    switch (m_column) {
    case 0:
        if (valueChanged(projectBean.getProjectName(), (String) value)) {
            projectBean.setProjectName((String) value);
        }
        break;
    case 1:
        if (valueChanged(projectBean.getProjectLocation(), (String) value)) {
            projectBean.setProjectLocation((String) value);
        }
        break;
    default:

    }
}

private boolean valueChanged(String previousValue, String currentValue) {

    boolean changed = false;
    if ((previousValue == null) && (currentValue != null)) {
        changed = true;
    } else if ((previousValue != null) && (currentValue != null) && (!previousValue.equals(currentValue))) {
        changed = true;
    }

    return changed;
}
}

class ProjectContentProvider2 implements org.eclipse.jface.viewers.IStructuredContentProvider {
    private org.eclipse.jface.viewers.TableViewer m_tableViewer;

    @Override
    public Object[] getElements(Object inputElement) {
        return (Object[]) inputElement;
    }

    public void dispose() {
    }

    public void inputChanged(org.eclipse.jface.viewers.Viewer viewer, Object oldInput, Object newInput) {
        m_tableViewer = (org.eclipse.jface.viewers.TableViewer) viewer;

    }

}

class ProjectColumnLabelProvider2 extends ColumnLabelProvider {

    private int m_column;

    public ProjectColumnLabelProvider2(int column) {
        m_column = column;
    }

    public String getText(Object element) {

        String text = null;
        if (element instanceof ProjectBean2) {
            ProjectBean2 projectBean = (ProjectBean2) element;
            switch (m_column) {
            case 0:
                text = projectBean.getProjectName();
                break;
            case 1:
                text = projectBean.getProjectLocation();
                break;
            default:
                // Nothing will happen
            }
        }

        return text;
    }

    public void update(ViewerCell cell) {
        if (m_column == 2) {
            TableItem item = (TableItem) cell.getItem();
            Button btn = new Button((Composite) cell.getControl(), SWT.NONE);
            btn.setText("A");
            btn.addSelectionListener(new SelectionAdapter() {
                @Override
                public void widgetSelected(SelectionEvent e) {
                    System.out.println(m_column + " Selected for open a folder!!!");
                }
            });

            TableEditor editor = new TableEditor(item.getParent());
            // editor.grabHorizontal = true;
            editor.horizontalAlignment = SWT.CENTER;
            editor.minimumWidth = btn.getSize().x + 20;
            editor.setEditor(btn, item, cell.getColumnIndex());
            editor.layout();

        }
    }

}

class ProjectBean2 {
    private String projectName;
    private String projectLocation;

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    public String getProjectLocation() {
        return projectLocation;
    }

    public void setProjectLocation(String projectLocation) {
        this.projectLocation = projectLocation;
    }

}

public class Test {
    private static void createTableViewer(Shell composite) {
        Table m_table = new Table(composite, SWT.FULL_SELECTION | org.eclipse.swt.SWT.MULTI | org.eclipse.swt.SWT.BORDER
                | org.eclipse.swt.SWT.V_SCROLL | org.eclipse.swt.SWT.H_SCROLL);
        m_table.setLinesVisible(true);
        m_table.setHeaderVisible(true);
        GridData tableData = new GridData(SWT.FILL, SWT.FILL, true, false, 2, 1);
        tableData.heightHint = 300;
        m_table.setLayoutData(tableData);
        org.eclipse.jface.viewers.TableViewerColumn columnViewer = null;

        String[] columnNames = { "Project Name", "Project Location", "" }; //$NON-NLS-1$

        TableViewer m_tableViewer = new TableViewer(m_table);

        for (int i = 0; i < columnNames.length; i++) {
            columnViewer = new org.eclipse.jface.viewers.TableViewerColumn(m_tableViewer, org.eclipse.swt.SWT.LEFT);
            columnViewer.getColumn().setText(columnNames[i]);

            if (columnNames[i].equals("")) { //$NON-NLS-1$
                columnViewer.getColumn().setWidth(25);
            } else if (columnNames[i].equals("Project Name")) { //$NON-NLS-1$
                columnViewer.getColumn().setWidth(150);
            } else if (columnNames[i].equals("Project Location")) { //$NON-NLS-1$
                columnViewer.getColumn().setWidth(150);
            }

            columnViewer.getColumn().setResizable(true);
            columnViewer.getColumn().setMoveable(true);

            // this line will call EditingSupport.setValue()
            columnViewer.setEditingSupport(new ProjectEditingSupport2(m_tableViewer, i));
            columnViewer.setLabelProvider(new ProjectColumnLabelProvider2(i));
        }

        m_tableViewer.setContentProvider(new ProjectContentProvider2());
        m_tableViewer.setColumnProperties(columnNames);

        List<ProjectBean2> beans = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            ProjectBean2 bean = new ProjectBean2();
            bean.setProjectLocation("Location" + i);
            bean.setProjectName("Name" + i);
            beans.add(bean);
        }

        m_tableViewer.setInput(beans.toArray());

    }

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display, SWT.SHELL_TRIM);
        shell.setText("Shawn Spiars' Table Edit Example");
        shell.setLayout(new FillLayout());

        createTableViewer(shell);

        shell.setSize(400, 400);
        shell.open();

        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
  }

visible data when clicked on editable cell

invisible data

1 个答案:

答案 0 :(得分:2)

您正在覆盖update的{​​{1}}方法,但没有调用ColumnLabelProvider,这将阻止显示数据。

使用

super.update

}