1

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!

EJoe
  • 55
  • 8

2 Answers2

3

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

Shashwat
  • 2,342
  • 1
  • 17
  • 33
  • Thank you very much for the link shared.I tried this and I am able to see the content as tool tip.In our requirement , User should have the provision to copy the content displayed in the hover text box.Could you please advice me how to achieve the same? – EJoe Apr 29 '19 at 11:15
0

I know it's a bit late but I have another solution. You can use the built in functions setToolTipText and getToolTipText. To copy I would insert a button that calls the getToolTipText function using Java clipboard manipulation something like here.

Noel Drotor
  • 43
  • 1
  • 6