考慮下面的Java(SWT)碼選擇和輸入之間的區別:SWT:在組合
private static ComboViewer createViewer(final Shell shell) {
final ComboViewer v = new ComboViewer(shell, SWT.DROP_DOWN);
v.setLabelProvider(new LabelProvider());
v.setContentProvider(new ArrayContentProvider());
v.setInput(new String[]{"value 1", "value 2"});
return v;
}
public static void main(final String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setSize(200, 60);
shell.setLayout(new GridLayout());
final ComboViewer v = createViewer(shell);
// This wires up the userSelectedSomething method correctly
v.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(final SelectionChangedEvent event) {
userSelectedSomething();
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
public static void userSelectedSomething() {
// This should be called *only if* the user selected from the drop-down
}
public static void userTypedSomething() {
// This should be called *only if* the user typed in the combo
}
我想打電話給userTypedSomething
方法,只有當用戶鍵入到組合(而不是當他們選擇從下拉菜單中)。我應該添加什麼樣的聽衆來實現這個目標?在組合查看器中使用v.getCombo().addModifyListener(...)
添加修改偵聽器是不行的,因爲這是從組合中鍵入和選擇觸發的。
可悲的是沒有。使用v.getCombo()。addVerifyListener添加驗證偵聽器會導致偵聽器被觸發(兩次!),以便從下拉列表中選擇以及一次用於鍵入。可以說這是JFace修復的錯誤... –
@DavidNorth再次檢查我的答案。我添加了一個代碼片段。你將不得不檢查事件的「文本」的長度。用戶輸入的長度應該等於1.正如我所說的,這不是最佳的,而是一個起點。 – Baz
@DavidNorth再次編輯答案。您可以使用'SWT.KeyUp'事件來觀看鍵盤輸入。 – Baz