SWT - ComboBoxCellEditor /默认值

时间:2012-09-10 17:50:02

标签: java combobox swt

我希望我的ComboBoxCellEditor能够进行3种选择。现在它只有Yes或No.我希望它有Yes,No,Both。

除非单击单元格,否则组合框选择值不会显示在表格中。除非他们点击空单元格,否则很难判断表格单元格是否可以进行选择。我希望它至少显示向下箭头 我已经阅读了一些可以解决这个问题的唯一方法是设置默认值。

  1. 我不确定如何添加第3个值。我将添加我的代码,试图添加第3个值

  2. 如何在没有首先点击单元格的情况下将组合框显示在表格中?

  3. public class OptionEditingSupport extends EditingSupport {
    
        private ComboBoxCellEditor cellEditor;
    
        public OptionEditingSupport(ColumnViewer viewer) {
            super(viewer);
            cellEditor = new ComboBoxCellEditor(((TableViewer)viewer).getTable(), new String[]{"Yes", "No", "Both"}, SWT.READ_ONLY);
    
        }
    
        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"); **<- Error Here
                ((AplotDatasetData)element).setMarkupValue(option);
                getViewer().update(element, null);
            }
        }
    }
    

2 个答案:

答案 0 :(得分:1)

conditional operator

x ? y : z

是一个三元运算符,在内部执行:

if(x)
    y;
else
    z;

因此,您只能将它与三个组件一起使用。请改用if else if else

Integer choice = (Integer)value;
String option = "";

if(choice == 0)
    option = "Yes";
else if(choice == 1)
    option = "No";
else
    option = "Both";

答案 1 :(得分:0)

TableEditor可用于在Table Cell上显示任何Widget。它应该通过显示Combobox来解决您的问题,让用户知道该行和列可以选择。

我不确定我理解你关于3种选择的问题。

相关问题