适用于Eclipse SWT Text的侦听器是什么? [英] What is the appropriate listener to use for Eclipse SWT Text?

查看:254
本文介绍了适用于Eclipse SWT Text的侦听器是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类型为 org.eclipse.swt.widgets.Text 。它生活在我正在开发的eclipse插件内。我想处理用户从该字段内选择文本的事件。那就是...在集中的同时,他们点击一些文本,拖动左或右选择文本。当我选择这个文本时,那就是我需要开始我的活动。

I have a class of this type org.eclipse.swt.widgets.Text. It lives inside of an eclipse plugin I am developing. I want to handle the event where the user selects text from within this field. That is... while focused, they click into some text and drag either left or right to select text. When this text is selected, that is when I need to fire my event.

我没有找到合适的听众来满足我的需要。 b $ b

I am yet unable to find the appropriate listener to cover my need.

推荐答案

我认为 SWT.MouseUp 是您要查找的事件。如果要查看箭头键和移位选择,请听 SWT.KeyUp ,并检查 keyCode 的事件:

I think SWT.MouseUp is the event you are looking for. If you want to check "arrow keys and shift selection", listen to SWT.KeyUp as well and check the keyCode of the event:

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, false));

    Text text = new Text(shell, SWT.BORDER);
    text.setText("BLABLABLA");

    text.addListener(SWT.MouseUp, new Listener() {

        @Override
        public void handleEvent(Event event) {
            Text text = (Text) event.widget;

            String selection = text.getSelectionText();

            if(selection.length() > 0)
            {
                System.out.println("Selected text: " + selection);
            }
        }
    });

    text.addListener(SWT.KeyUp, new Listener() {

        @Override
        public void handleEvent(Event event) {
            Text text = (Text) event.widget;

            String selection = text.getSelectionText();

            if(selection.length() > 0 && event.keyCode == SWT.SHIFT)
            {
                System.out.println("Selected text: " + selection);
            }
        }
    });

    shell.pack();
    shell.open ();
    while (!shell.isDisposed ()) {
        if (!display.readAndDispatch ()) display.sleep ();
    }
    display.dispose ();
}

此代码只会打印选择,如果用户真的选择了某些东西。如果它是一个没有选择的鼠标向上键,不会发生任何事情。

This code will only print the selection, if the user really selected something. If it's a mouse-up o key-up without selection, nothing will happen.

您可能希望在一个监听器中组合以节省空间。

You might want to combine both in one listener to save space.

这篇关于适用于Eclipse SWT Text的侦听器是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆