Search code examples
javaeclipse-pluginswt

How to reset Combo when SWT.READ_ONLY


I am using the org.eclipse.swt.widgets.Combo class and I am doing the following

Combo myCombo = new Combo(container, SWT.READ_ONLY);
myCombo.add("1");
myCombo.add("2");

//later on
myCombo.setText(""); //will not work because READ_ONLY

The user will choose an element of the combo, and I am providing a reset button where I want the value to be set to null. However, according to the javadoc, the setText method is ignored when the receiver is READ_ONLY. I like the Combo being read only because I only want the user to select what I provide.. But I want to set the value back to null or "" if possible. Can I do this with a the read only receiver? Or what is another good way of doing this?

Thanks!


Solution

  • Use combo.deselectAll() to reset the selection.

    Alternatively you could use:

    combo.deselect(combo.getSelectionIndex());
    

    In both cases getSelectionIndex() will return -1 afterwards.

    These methods appear as if the Combo supports multi-selection, which it doesn't. However strange they may appear, they do reset the selection.