Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Tab-key on a multiline text filed 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?

like image 528
G B Avatar asked Jan 29 '10 11:01

G B


1 Answers

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;
        }
    }
});
like image 76
Simon Avatar answered Oct 07 '22 16:10

Simon