Jakob Sachs
Jakob Sachs

Reputation: 715

SWT Eclipse Combo Event

I'm doing an Eclipse plugin with an Form MultiPage Editor.

On one of the pages I have split the page in two halves and are generating the pages in two different classes. In the FormPage im adding these two halves and everything is fine.

Now to my problem: On each side I have one Combo Box which is set to READ_ONLY. The Problem is that the Items of the second Combo are dependent on the selected Item from the first Combo.

Small mockup of my Code:

//something 

new FirstHalf(Stuff);

new SecondHalf(OtherStuff);

----------
public int firstComboIndex = 0;

public FirstHalf(Stuff){

    Combo firstCombo = new Combo(SomeClient, SWT.READ_ONLY);

    String[] itemsArray = new String[stuff];

    firstCombo.setItems(itemsArray);

    firstCombo.setText(itemsArray[firstComboIndex]);

}

----------
public int secondComboIndex = 0;

public SecondHalf(Stuff){

    Combo secondCombo = new Combo(SomeOtherClient, SWT.READ_ONLY);

    String[] array1 = new String[stuff];
    String[] array2 = new String[stuff];
    String[] array3 = new String[stuff];

    String[][] arrays = { array1, array2, array3};

    String[] secondItemsArray = new String[arrays[firstComboIndex];

    secondCombo.setItems(secondItemsArray);

    secondCombo.setText(secondItemsArray[secondComboIndex]);

}

Now how do I do it so, that when ever the first combo choice is changed. The second changes too.

Upvotes: 0

Views: 1674

Answers (1)

greg-449
greg-449

Reputation: 111142

Just use a selection listener on the first combo to call setItems on the second.

For example:

Combo firstCombo = new Combo(parent, SWT.READ_ONLY);

String[] itemsArray = {"1", "2", "3"};

firstCombo.setItems(itemsArray);

firstCombo.select(0);

Combo secondCombo = new Combo(parent, SWT.READ_ONLY);

String[] array1 = {"1a", "1b"};
String[] array2 = {"2a", "2b"};
String[] array3 = {"3a", "3b"};

String[][] arrays = {array1, array2, array3};

secondCombo.setItems(arrays[0]);

secondCombo.select(0);

// Selection listener to change second combo

firstCombo.addSelectionListener(new SelectionAdapter()
  {
    @Override
    public void widgetSelected(final SelectionEvent event)
    {
      int index = firstCombo.getSelectionIndex();

      secondCombo.setItems(arrays[index]);

      secondCombo.select(0);
    }
  });

Upvotes: 2

Related Questions