To track the user's selection (DefaultSelection) : Combo Event « SWT « Java Tutorial






To track the user's selection (DefaultSelection)
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class ComboSelectionEvent {

  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    String[] ITEMS = { "A", "B", "C", "D" };

    final Combo combo = new Combo(shell, SWT.DROP_DOWN);
    combo.setItems(ITEMS);

    combo.addSelectionListener(new SelectionListener() {
      public void widgetSelected(SelectionEvent e) {
        System.out.println("Selected index: " + combo.getSelectionIndex() + ", selected item: "
            + combo.getItem(combo.getSelectionIndex()) + ", text content in the text field: "
            + combo.getText());
      }

      public void widgetDefaultSelected(SelectionEvent e) {
        System.out.println("Default selected index: "
            + combo.getSelectionIndex()
            + ", selected item: "
            + (combo.getSelectionIndex() == -1 ? "<null>" : combo
                .getItem(combo.getSelectionIndex())) + ", text content in the text field: "
            + combo.getText());
      }
    });

    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}
  1. The widgetSelected method of the selection listener is called when the combo's list selection changes.
  2. The widgetDefaultSelected method is typically called when the user presses the Enter key in the combo's text field.








17.12.Combo Event
17.12.1.Add selection listener to ComboAdd selection listener to Combo
17.12.2.Combo default selection event (Return/Enter key)Combo default selection event (Return/Enter key)
17.12.3.To track the user's selection (DefaultSelection)To track the user's selection (DefaultSelection)
17.12.4.Add TraverseListener to ComboAdd TraverseListener to Combo
17.12.5.Disable Tab key to transfer focus for ComboDisable Tab key to transfer focus for Combo