2017-08-23 69 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); 
    } 
    });