Chris Bolton
Chris Bolton

Reputation: 2314

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!

Upvotes: 4

Views: 2506

Answers (2)

Rüdiger Herrmann
Rüdiger Herrmann

Reputation: 21025

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.

Upvotes: 5

Zhar
Zhar

Reputation: 3540

The best way is to use ComboViewer

    List<String> input = new ArrayList<String>();
    input.add("1");
    input.add("2");
    combo = new ComboViewer(container, SWT.READ_ONLY);
    combo.setLabelProvider(new LabelProvider());
    combo.setContentProvider(ArrayContentProvider.getInstance());
    combo.setInput(input);

And to clear it

combo.setSelection(StructuredSelection.EMPTY);

Regards

Upvotes: 0

Related Questions