User Tools

Site Tools


override_default_action_for_swt_text_field

Override Default Action for SWT Text Field

Normally the default button will be triggered when the enter key is pressed in a text field and the text field will loose the focus.

This can be change/overridden:

    Text text = new Text(shell, SWT.SINGLE | SWT.BORDER);
    text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
    text.setText("Here is some text");
    text.addSelectionListener(new SelectionAdapter() {
      public void widgetDefaultSelected(SelectionEvent e) {
        System.out
            .println("Text default selected (overrides default button)");
      }
    });
    text.addTraverseListener(new TraverseListener() {
      public void keyTraversed(TraverseEvent e) {
        if (e.detail == SWT.TRAVERSE_RETURN) {
          e.doit = false;
          e.detail = SWT.TRAVERSE_NONE;
        }
      }
    });

override_default_action_for_swt_text_field.txt · Last modified: 2013/12/09 14:17 by mihael