We are trying show a hover box when user place the mouse over the SWT text box. 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.

We are using Eclipse oxygen IDE and the explained feature is for a standalone Eclipse RCP application

Thanks in Advance!

1 Answers

3
Shashwat On

Please go though this tutorial for hover text on 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

Output