java - 如何从 SWT TextBox 获取当前输入/修改的单词

标签 java swt spell-checking

我正在用 java 编写拼写检查器功能。我面临着一个麻烦, 当我在 Text 中输入一些文本时。从中我如何获取当前输入或当前修改的单词,以便我可以 验证当前输入或修改的单词是否在我的词典中找到。

我已经通过用红色下划线突出显示了字典中找不到的单词。但我是通过阅读整个文本的每次修改来实现这一点的。

最佳答案

我设法将一些代码放在一起,但不会迭代整个文本。相反,它从当前光标位置左右移动,搜索单词的结尾。当找到两端时,输出单词:

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

    Text text = new Text(shell, SWT.BORDER);
    GridData data = new GridData(SWT.FILL, SWT.BEGINNING, true, true);
    data.horizontalSpan = 2;
    text.setLayoutData(data);
    new Label(shell, SWT.NONE).setText("Current word:");
    final Label label = new Label(shell, SWT.NONE);

    text.addListener(SWT.Verify, new Listener()
    {
        @Override
        public void handleEvent(Event e)
        {
            Text source = (Text) e.widget;

            /* Construct the entered text */
            String oldString = source.getText();
            String textString = oldString.substring(0, e.start) + e.text + oldString.substring(e.end);

            /* Get all the chars */
            char[] text = textString.toCharArray();

            /* Get the cursor position */
            int position = source.getCaretPosition();

            /* Adjust cursor position based on input (necessary for delete operations) */
            if(e.text.equals(""))
                position--;
            else
                position++;

            /* Remember start and end of current word */
            int leftBorder = -1;
            int rightBorder = -1;

            /* Search for left end of the current word */
            for(int i = 1; i < position; i++)
            {
                int left = position - i;

                if(left > 0)
                {
                    if(!Character.isLetter(text[left]))
                    {
                        leftBorder = left + 1;
                        break;
                    }
                }
            }

            /* Search for right end of the current word */
            for(int i = position; i < text.length; i++)
            {
                int right = i;

                if(right < text.length)
                {
                    if(!Character.isLetter(text[right]))
                    {
                        rightBorder = right;
                        break;
                    }
                }
            }

            /* If the word is the first/last, set border accordingly */
            if(leftBorder == -1)
                leftBorder = 0;
            if(rightBorder == -1)
                rightBorder = text.length;

            StringBuilder result = new StringBuilder();
            /* Output the word */
            for(int i = leftBorder; i < rightBorder; i++)
                result.append(text[i]);

            label.setText(result.toString());
            shell.layout(true, true);
        }
    });

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

以下是突出显示当前编辑的单词的两个屏幕截图:

enter image description here

enter image description here

<小时/>

因此,如果整个文本的长度为 n 并且当前单词的长度为 m,则运行时间将为 O(m) O(n)

关于java - 如何从 SWT TextBox 获取当前输入/修改的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19013209/

相关文章:

java - 将多个集合组合成一个逻辑集合?

java - 基于 SWT 的 GUI 在从全尺寸最小化状态最大化时变黑

java - Find out if the calling thread is the SWT UI thread - 确定调用线程

java - 将文本包裹在按钮内

c# - WPF 拼写检查终结器 - 安全句柄已关闭

java - 如何让 Gui 真正提示对话框?

java - 访问网页时出现问题(在 Wildfly 上上传)

Solr 你的意思是(拼写检查组件)

java - 打开 Office 拼写检查器/Java API

java - Swing 文本字段有时出现有时不出现?