ComboViewer,如何获取comboViewer的选定项?

时间:2014-07-19 14:41:30

标签: java get swt viewer

我创建了一个ComboViewer

    final ComboViewer comboViewer = new ComboViewer(shlFreeViews, SWT.NONE);
    final Combo combo = comboViewer.getCombo();
    combo.setVisibleItemCount(4);
    combo.setFont(SWTResourceManager.getFont("Segoe UI", 13, SWT.NORMAL));
    combo.setItems(new String[] {"5", "10", "15", "20"});
    combo.setBounds(356, 172, 126, 25);
    combo.setText("5");

问题是,当我打开程序时,我必须选择一些我的comboviewer,然后我必须使用这个数字做一个简单的添加。获取所选编号的comboviewer的代码是什么?

1 个答案:

答案 0 :(得分:6)

您可以向ISelectionChangedListener添加ComboViewer,以便在选择更改时收到通知,或者您可以手动获取。程序是一样的:

StructuredSelection sel = (StructuredSelection) viewer.getSelection();
YourDataType element = (YourDataType) sel.getFirstElement();