我正在使用 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]);
}
现在我该怎么做,当第一个组合选择改变时。第二个也发生了变化。
最佳答案
只需在第一个组合上使用选择监听器即可在第二个组合上调用 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);
}
});
关于java - SWT Eclipse 组合事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45842307/