4

I am writing an RCP application in eclipse that contains a combobox, and upon selecting any of its items, a selection event is being fired and some random code comes in action. The listener looks something like this:

randomComboBox.addSelectionListener(new SelectionListener(){

    @Override
    public void widgetSelected(SelectionEvent e) {
        // random code
    }

    @Override
    public void widgetDefaultSelected(SelectionEvent e) {
        // TODO Auto-generated method stub
    }

});

My question is: is it possible to fire the event from the code? For example if I add:

randomComboBox.select(0);

no event is being fired. In this case, do I have to write my own listener?

Keppil
  • 45,603
  • 8
  • 97
  • 119
deckard cain
  • 497
  • 10
  • 24

2 Answers2

4

The select method of the combo box sends an event of the type SWT.Modify when it changes the selection, so you could use a ModifyListener instead of a SelectionListener.

Actually, the ModifyListener listens to changes in the text field of the combo box, this means it reacts to the text change that is caused by the selection. This also means that it will be fired if that text is changed by other paths (e.g. user entries in the combo text field).

Keeping that behaviour in mind, a ModifyListener might be an option.

Modus Tollens
  • 5,083
  • 3
  • 38
  • 46
  • 6
    Thanks for your reply, ModifyListener does the job. I found one way to do it with SelectionListener and that is by using randomComboBox.notifyListeners(SWT.Selection, new Event()) after changing selection from code. In any case ModifyListener is prettier :] – deckard cain Sep 25 '12 at 11:31
  • 1
    @deckardcain I like your solution with the notifyListeners – RyPope Jun 07 '14 at 00:14
  • 1
    @deckardcain Revisiting this question, I believe your solution using notifyListeners is much cleaner. You should add it as an answer and accept it. – Modus Tollens Jun 07 '14 at 05:14
4

Do not use ModifyListener if your ComboBox is "Read Only"

Combo comboBoxColor = new Combo(composite, SWT.BORDER | SWT.READ_ONLY);

You can fire any event explicitly (Programmatically) For e.g.

control.notifyListeners(eventType , event);

In your case:

comboBoxColor.notifyListeners(SWT.Selection, new Event())
  1. SWT.Selection -> Event type, you can get all event constants from SWT class.

  2. new Event() -> Event object

Shramik
  • 81
  • 8