我有ComboViewer
。在看到下拉的同时,我在查看器外部点击了鼠标(而不是在查看器的任何部分)。下拉列表中的第一项填充在框中。当这个人群发生时会产生什么事件?
我在comboviewer上尝试了SelectionChangedListener
,在浏览器的组合框上尝试了FocusListener
,KeyListener
和SelectionListener
。这些都没有帮助。
我的要求是根据组合框中的内容启用和禁用按钮。组合为空,禁用按钮,否则启用它。
public class CartoonAnimalCombo {
public class Cartoon {
private String firstName;
private String type;
public Cartoon(String firstName, String lastName) {
this.firstName = firstName;
this.type = lastName;
}
public String getFirstName() {
return firstName;
}
public String getType() {
return type;
}
}
public static void main(String[] argv) {
new CartoonAnimalCombo();
}
private String inputText;
CartoonAnimalCombo() {
Display d = new Display();
Shell parent = new Shell(d);
parent.setSize(250, 250);
GridLayout layout = new GridLayout(2, false);
parent.setLayout(layout);
Label label = new Label(parent, SWT.NONE);
label.setText("Select a character:");
final ComboViewer viewer = new ComboViewer(parent, SWT.NONE);
viewer.setContentProvider(ArrayContentProvider.getInstance());
viewer.setLabelProvider(new LabelProvider() {
@Override
public String getText(Object element) {
if (element instanceof Cartoon) {
Cartoon person = (Cartoon) element;
return person.getFirstName();
}
return super.getText(element);
}
});
final Cartoon[] cartoons = new Cartoon[] { new Cartoon("Mickey", "Mouse"), new Cartoon("Donald", "Duck"),
new Cartoon("Pluto", "Dog"), new Cartoon("Chip", "Squirrel"), new Cartoon("Dale", "Squirrel") };
viewer.setInput(cartoons);
final ArrayList<Cartoon> tempVar = new ArrayList<Cartoon>();
final Combo combo = viewer.getCombo();
combo.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
if (e.keyCode != SWT.ARROW_DOWN && e.keyCode != SWT.ARROW_UP && e.keyCode != SWT.CR) {
tempVar.clear();
String text = combo.getText();
if (text != null && !text.isEmpty()) {
setSearchText(text);
for (Cartoon var : cartoons) {
if (var.getFirstName().matches(inputText)) {
tempVar.add(var);
}
}
}
if (text.isEmpty()) {
tempVar.addAll(Arrays.asList(cartoons));
}
viewer.setInput(tempVar.toArray());
combo.setListVisible(true);
combo.setText(text);
combo.setSelection(new Point(text.length(), text.length()));
}
}
});
Label lastName = new Label(parent, SWT.NONE);
lastName.setText("Type of Animal:");
final Text textbox = new Text(parent, SWT.BORDER);
viewer.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
IStructuredSelection selection = (IStructuredSelection) event.getSelection();
if (selection.size() > 0) {
textbox.setText(((Cartoon) selection.getFirstElement()).getType());
}
}
});
parent.open();
while (!parent.isDisposed()) {
if (!d.readAndDispatch()) {
d.sleep();
}
}
d.dispose();
}
public void setSearchText(String searchText) {
if (searchText == null || searchText.isEmpty()) {
this.inputText = ".*"; //$NON-NLS-1$
} else {
this.inputText = searchText + ".*"; //$NON-NLS-1$
}
}
}