Obay
Obay

Reputation: 3205

Java SWT: How to trigger ModifyListener through source code?

I have attached a ModifyListener to a Combo box and it works fine. But how do I trigger it through source code? Is there a better way than this?:

int selected = myCombo.getSelectionIndex();
myCombo.select(selected + 1);
myCombo.select(selected);

Upvotes: 0

Views: 1204

Answers (1)

Simon
Simon

Reputation: 706

Programatically triggering a ModifyEvent in order to perform some GUI update (which I assume is what you're trying to do) is not really a good design.

Better to split the functionality you want to call out into a separate function and call it directly. Something like this:

private void doSomething() {
  // TODO: Something!
}

....

myCombo.addModifyListener(new ModifyListener(){

public void modifyText(ModifyEvent arg0) {
  doSomething();
}});

doSomething();

Any arguments you need to provide to your doSomething() method should be available without a ModifyEvent.

Hope this helps.

Upvotes: 7

Related Questions