【问题标题】:SWT - Using EditingSupport in my TableViewerSWT - 在我的 TableViewer 中使用 EditingSupport
【发布时间】:2012-10-11 19:49:46
【问题描述】:

在我的主要 dailog 中,我有一个 JFace TableViewer。表格的最后一列是 ComboBoxCellEditor。他们可以选择“否”、“是”、“两者”。这一切都按设计工作。

但这是我的问题。

  1. 如果用户选择两者作为值。
  2. 我需要运行一个从数组中获取当前行数据的方法
  3. 将两者的值更改为否
  4. 复制数据,然后将值更改为是
  5. 将两者重新添加到数组中
  6. 刷新表格

表格示例

来自-

1002 | 001   | sss   |  part | both(user changed from default)

到 -

1002 |  001  |  sss  |  part |  No

1002 |  001  |  sss  |  part | Yes

我试图弄清楚在选择两者之后如何运行该方法来完成其余的工作。我假设它必须是某种听众。请查看我的 EditingSupport 代码并告诉我从哪里开始我的方法来完成剩下的工作。

public class OptionEditingSupport extends EditingSupport 
{
    private ComboBoxCellEditor cellEditor;

    public OptionEditingSupport(ColumnViewer viewer) {
        super(viewer);
        cellEditor = new ComboBoxCellEditor(((TableViewer)viewer).getTable(), new String[]{"No", "Yes", "Both"}, SWT.DROP_DOWN);
        //cellEditor.setValue(0);
    }
    protected CellEditor getCellEditor(Object element) {
        return cellEditor;
    }
    protected boolean canEdit(Object element) {
        return true;
    }
    protected Object getValue(Object element) {
        return 0;
    }
    protected void setValue(Object element, Object value) 
    {
        if((element instanceof AplotDatasetData) && (value instanceof Integer)) {
            Integer choice = (Integer)value;
            //String option = (choice == 0? "Yes":"No":"Both");
            String option = ((AplotDatasetData)element).getMarkupValue();;
            if(choice == 0) {
                option = "No";
            }    
            else if(choice == 1) {
                option = "Yes";
            }    
            else {
                option = "Both";
            }    
            ((AplotDatasetData)element).setMarkupValue(option);
            getViewer().update(element, null);
        }
    }
}  

【问题讨论】:

    标签: java swt jface tableviewer


    【解决方案1】:

    据我了解您的问题,您想复制一个对象,将其添加到您的模型中并刷新查看器。

    当用户在组合框中选择"both" 时,这一切都应该发生。你已经知道,当这种情况发生时。您最终会遇到setValue 方法的else 情况。然后你可以在那里做你必须做的事情:

    protected void setValue(Object element, Object value) 
    {
        if((element instanceof AplotDatasetData) && (value instanceof Integer)) {
            Integer choice = (Integer)value;
    
            String option = ((AplotDatasetData)element).getMarkupValue();
    
            if(choice == 0) {
                option = "No";
            }    
            else if(choice == 1) {
                option = "Yes";
            }    
            else {
                option = "Both";
    
                // create a copy of your element
                // add it to your model
                // update the viewer
            }
    
            getViewer().update(element, null);
        }
    
    }
    

    【讨论】:

    • 这很明显,您只需指出即可。巴兹非常感谢您的所有帮助。我从你的建议和解释中学到了很多
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多