SWT Eclipse组合活动

时间:2017-08-23 14:17:05

标签: java eclipse swt

我正在使用Form MultiPage Editor进行Eclipse插件。

在其中一个页面上,我将页面分成两半,并在两个不同的类中生成页面。在FormPage中添加这两半,一切都很好。

现在我的问题:在每一方面我都有一个设置为READ_ONLY的组合框。问题是第二个组合的项目取决于第一个组合中的选定项目。

我的代码的小模型:

//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]);

}

现在我该怎么做呢,当第一个组合选择被改变时。第二个也发生了变化。

1 个答案:

答案 0 :(得分:2)

只需在第一个组合上使用选择侦听器就可以在第二个组合上调用setItems

例如:

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);
    }
  });
相关问题