4

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?

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
xSmog3s
  • 119
  • 1
  • 3
  • 8

1 Answers1

6

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();
Baz
  • 34,567
  • 11
  • 66
  • 88
  • @user3850946 You can [edit](http://stackoverflow.com/posts/24841539/edit) your question and add code. – Baz Jul 19 '14 at 15:33
  • 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"); StructuredSelection sel = (StructuredSelection) viewer.getSelection(); YourDataType element = (YourDataType) sel.getFirstElement(); – xSmog3s Jul 20 '14 at 18:54
  • @user3850946 Please edit your question and add the code there. And shouldn't you know best if it's correct and working? – Baz Jul 20 '14 at 19:50