2017-08-23 29 views
0

我正在做一个Form MultiPage Editor的Eclipse插件。SWT 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]); 

} 

现在我该怎样做,所以,当有史以来第一个组合的选择而改变。第二个也在改变。

+0

尝试'SelectionListener' ... –

回答

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); 
    } 
    });