xSmog3s
xSmog3s

Reputation: 119

ComboViewer, How can I get the selected item of a comboViewer?

I created a 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");

The problem is that when I open the program I have to select a number of my comboviewer and then I have to use this number for do a simple addition. What the code to get the selected number of the comboviewer?

Upvotes: 4

Views: 3088

Answers (1)

Baz
Baz

Reputation: 36894

You can either add an ISelectionChangedListener to the ComboViewer to get notified when the selection changes, or you can get it manually. The procedure is the same:

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

Upvotes: 6

Related Questions