我想以编程方式为ComboViewer设置一个值。我试图从对象中获取值并使用setSelection(Iselection)以这种方式设置viewer.setSelection(新的StructuredSelection(Object),但是值不会在ComboViewer Selection上更新。可以任何一个帮助我如何以编程方式设置ComboViewer的选择?
答案 0 :(得分:4)
我最近这样做了。如果组合中设置的值和您设置的值匹配,则需要关注。尝试打印或单步执行。
这是需要做的事情 -
// set up comboViewer
comboViewer = new ComboViewer(leftSectionComposite, SWT.READ_ONLY);
comboViewer.setContentProvider(new ArrayContentProvider());
comboViewer.setLabelProvider(new LabelProvider()); // Use your label provider if possible.
comboViewer.setInput(YOURVALUES);
// set value
final ISelection selection = new StructuredSelection(valueFromComboThatYouWantToSet);
comboViewer.setSelection(selection);
答案 1 :(得分:0)
// set up comboViewer
comboViewer = new ComboViewer(leftSectionComposite, SWT.READ_ONLY);
comboViewer.setContentProvider(new ArrayContentProvider().getInstance());
comboViewer.setLabelProvider(new LabelProvider() {
@Override
public String getText(Object element) {
if (element instanceof OwnerClass)
return ((OwnerClass)element).getName();
return super.getText(element);
}
}););
comboViewer.setInput(getService().getOwnerClass());
Person p = (Person)selection.getFirstElement();
final ISelection selection = new StructuredSelection(p.getOwnerClassObj().getName());
comboViewer.setSelection(selection);
答案 2 :(得分:0)
public enum CarType {
SUV, MPV, Hatchback, Sedan;
}
// ...
cv = new ComboViewer(aComposite);
cv.setContentProvider(ArrayContentProvider.getInstance());
cv.setInput(CarType.values());
//THE BELOW LINE HAVE WHAT YOU WANT
final ISelection defaultValue = new StructuredSelection(CarType.Sedan);
cv.setSelection(defaultValue);