我正在尝试使用ComboBoxCellEditor列创建一个表。当我设置值时,低于异常的时间就要到了。
import org.eclipse.jface.util.Policy;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
/ ............ /
公共课程测试{ 公共静态void main(String [] args){ Shell shell = new Shell(); shell.setText(“ TableViewer示例”); GridLayout layout = new GridLayout(); shell.setLayout(layout); Composite复合=新的Composite(shell,SWT.NONE); Composite.setLayoutData(new GridData(SWT.FILL,SWT.CENTER,true,true)); Composite.setLayout(new GridLayout(1,false)); 表testTable =新表(复合,SWT.BORDER); testTable.setLinesVisible(true); testTable.setHeaderVisible(true); GridData tableData =新的GridData(SWT.FILL,SWT.FILL,true,false); tableData.heightHint = 300; testTable.setLayoutData(tableData); TableViewerColumn columnViewer = null; String [] columnNames = {“名称”,“笔记本电脑”}; TableViewer testTableViewer =新的TableViewer(testTable); for(int i = 0; i
/ ............. /
TestBean类{ 私有字符串名称; 私人String笔记本电脑; public String [] getListOfLaptop = {“ Acer”,“ HP”,“ Lenovo”,“ Dell”,“ Benq” }; // getter和setter方法 }
/ .................... /
class TestEditingSuport extends EditingSupport {
private int m_column;
private CellEditor m_editor;
public TestEditingSuport(ColumnViewer viewer, int column,
String[] listOfTestBean) {
super(viewer);
m_column = column;
// Create the correct editor based on the column index
switch (column) {
case 0:
case 1:
m_editor = new ComboBoxCellEditor(
((TableViewer) viewer).getTable(), listOfTestBean);
break;
default:
}
}
@Override
protected CellEditor getCellEditor(Object element) {
return m_editor;
}
@Override
protected boolean canEdit(Object element) {
return true;
}
@Override
protected Object getValue(Object element) {
TestBean bean = (TestBean) element;
Object value = null;
switch (m_column) {
case 0:
value = bean.getName();
break;
case 1:
value = bean.getLaptop();
break;
default:
}
return value;
}
@Override
protected void setValue(Object element, Object value) {
TestBean bean = (TestBean) element;
switch (m_column) {
case 0:
if (valueChanged(bean.getName(), (String) value)) {
bean.setName((String) value);
}
getViewer().update(bean, null);
break;
case 1:
int index = (Integer) value;
String laptop = bean.getListOfLaptop[index];
if (valueChanged(bean.getLaptop(), laptop)) {
bean.setLaptop(laptop);
}
getViewer().update(bean, null);
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 TestContentProvider implements IStructuredContentProvider {
@Override
public Object[] getElements(Object inputElement) {
return (Object[]) inputElement;
}
}
/ ......................... /
class TestColumnLabelProvider extends ColumnLabelProvider {
private int m_column;
public TestColumnLabelProvider(int column) {
this.m_column = column;
}
public String getText(Object element) {
String text = null;
if (element instanceof TestBean) {
TestBean testBean = (TestBean) element;
switch (m_column) {
case 0:
text = testBean.getName();
break;
case 1:
text = testBean.getLaptop();
break;
default:
}
}
return text;
}
}
/ ...................... /
答案 0 :(得分:2)
ComboCellEditor
值是在构造函数中提供的值列表的整数索引。
您的getValue
类的EditingSupport
方法必须在值列表中返回一个Integer
索引。
您的setValue
类的EditingSupport
方法将被赋予一个Integer
,其中包含选定的索引。