简体   繁体   English

如何显示 swt 文本框的悬停文本

[英]How to show hover text for a swt text box

We are trying show a hover box when user place the mouse over the SWT text box.当用户将鼠标放在 SWT 文本框上时,我们尝试显示一个悬停框。 From the hover box user will be able to copy the content.This is the requirement.从悬停框用户将能够复制内容。这是要求。

Please help me by providing an example to implement the same.Or please give an insight how eclipse is implemented the same in the editors.请通过提供一个示例来帮助我实现相同的内容。或者请深入了解 eclipse 是如何在编辑器中实现的。

We are using Eclipse oxygen IDE and the explained feature is for a standalone Eclipse RCP application我们正在使用 Eclipse 氧气 IDE,所解释的功能适用于独立的 Eclipse RCP 应用程序

Thanks in Advance!提前致谢!

Please go though this tutorial for hover text on SWT Text .请阅读本教程,了解SWT Text上的悬停SWT Text

Below code is modified version of code of that tutorial下面的代码是该教程代码的修改版本

package test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class Example {

    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);

        GridLayout gridLayout = new GridLayout(2, true);
        shell.setLayout(gridLayout);

        Label label1 = new Label(shell, SWT.NONE);
        label1.setText("First Name");
        Text text1 = new Text(shell, SWT.BORDER);
        Label label2 = new Label(shell, SWT.NONE);
        label2.setText("Last Name");
        Text text2 = new Text(shell, SWT.BORDER);
        shell.pack();

        final HoverShell hShell = new HoverShell(shell);
        text1.addListener(SWT.MouseHover, new Listener() {

            @Override
            public void handleEvent(Event event) {
                hShell.text.setText("Enter First Name");
                hShell.hoverShell.pack();
                hShell.hoverShell.open();
            }
        });

        text1.addListener(SWT.MouseExit, new Listener() {

            @Override
            public void handleEvent(Event event) {
                hShell.hoverShell.setVisible(false);
            }
        });
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }

}

class HoverShell {
    Shell hoverShell;
    Text text;

    public HoverShell(Shell shell) {
        hoverShell = new Shell(shell, SWT.ON_TOP | SWT.TOOL);
        hoverShell.setLayout(new FillLayout());
        text = new Text(hoverShell, SWT.NONE);
        text.setBackground(hoverShell.getBackground());
        text.setEditable(false);
    }
}

Output输出

输出

I know it's a bit late but I have another solution.我知道这有点晚了,但我有另一个解决方案。 You can use the built in functions setToolTipText and getToolTipText.您可以使用内置函数 setToolTipText 和 getToolTipText。 To copy I would insert a button that calls the getToolTipText function using Java clipboard manipulation something like here .要复制,我将插入一个按钮,该按钮使用 Java 剪贴板操作调用 getToolTipText 函数,类似于此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM