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

折月煮酒 提交于 2020-01-03 08:18:16

问题


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?


回答1:


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;
        }
    }
});



回答2:


Add a keystroke listener to the textfield or textarea and implement a special handler for TAB. I've seen this 'tab catching' on Swing applications too and it's usually annoying.

You could offer another handler for Shift-TAB which then would insert a \t, if you still want to allow tabs in the textfield.



来源:https://stackoverflow.com/questions/2161714/use-tab-key-on-a-multiline-text-filed-in-swt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!