javatextswttabsmultiline

Use Tab-key on a multiline text field in swt?


How do I prevent a multi-line text field from "stealing" tab-key presses?

I mean: I'd like to use TAB to cycle between the elements of a window, but when I enter the multiline text, TAB becomes a "normal" key, and simply inserts tabulators into the text I'm typing.

How do I handle this? Should I write some custom listener, or can I change the behaviour of the component by using an SWT constant?


Solution

  • SWT defines a mechanism for implementing this kind of behaviour using the TraverseListener class. The following snippet (taken from here) shows how:

    Text text1 = new Text(shell, SWT.MULTI | SWT.WRAP);
    text1.setBounds(10,10,150,50);
    text1.setText("Tab will traverse out from here.");
    text1.addTraverseListener(new TraverseListener() {
        public void keyTraversed(TraverseEvent e) {
            if (e.detail == SWT.TRAVERSE_TAB_NEXT || e.detail == SWT.TRAVERSE_TAB_PREVIOUS) {
                e.doit = true;
            }
        }
    });