简体   繁体   中英

SWT Display Popup on TreeItem MouseHover

I'm trying to display a popup (simple at first, but with buttons afterwards) upon mouse hover on a TreeItem element.

Here's my Code:

   Display.getDefault().asyncExec(new Runnable() {
                        @Override
                        public void run() {

                            int x = treeItem.getBounds().x, y = treeItem.getBounds().y+30;
                            try {
                            Composite composite = new Composite(parentComposite, SWT.EMBEDDED | SWT.NO_BACKGROUND);
                                Frame frame = SWT_AWT.new_Frame(composite);
                                JFrame jframe = new JFrame();
                                frame.add(jframe);


                            PopupFactory.getSharedInstance().getPopup(jframe, new JLabel("Howdy"), x, y).show();

                                    Thread.sleep(2000);
                                } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }

                        }
                    });

Somehow, the code doesn't get past the frame.add(jframe). What could be the cause?

Cheers, Mihai

Here is simple example:

import java.awt.Frame;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Popup;
import javax.swing.PopupFactory;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class Test {

    private Popup popup;

    public Test() {
        Display d = new Display();
        final Shell shell = new Shell(d);
        final Button b = new Button(shell, SWT.PUSH);
        b.setText("test");
        b.addListener(SWT.MouseEnter, new Listener() {
            public void handleEvent(Event e) {
                callPopUp(e,shell);
            }
        });
        b.addListener(SWT.MouseExit, new Listener() {
            public void handleEvent(Event e) {
                hidePopUp();
            }
        });
        b.pack();
        shell.pack();
        shell.open();

        while (!shell.isDisposed()) {
            if (!d.readAndDispatch()) {
                d.sleep();
            }
        }
        d.dispose();
    }

    private void hidePopUp() {
        if(popup != null){
            popup.hide();
        }
    }

    private void callPopUp(Event e, Shell shell) {
        Composite composite = new Composite(shell, SWT.EMBEDDED| SWT.NO_BACKGROUND);
        Frame frame = SWT_AWT.new_Frame(composite);
        JPanel p = new JPanel();
        frame.add(p);
        popup = PopupFactory.getSharedInstance().getPopup(p, new JLabel("Howdy"), shell.getBounds().x + 20,shell.getBounds().y+20);
        popup.show();
    }

    public static void main(String[] args) {
        new Test();
    }
}

在此处输入图片说明

Here is a pure SWT solution:

private static Shell popup;

public static void main(String[] args)
{
    Display d = new Display();
    final Shell shell = new Shell(d);
    shell.setLayout(new FillLayout());
    final Button button = new Button(shell, SWT.PUSH);
    button.setText("test");

    button.addListener(SWT.MouseEnter, new Listener()
    {
        public void handleEvent(Event e)
        {
            callPopUp(e, shell);
        }
    });
    button.addListener(SWT.MouseExit, new Listener()
    {
        public void handleEvent(Event e)
        {
            hidePopUp();
        }
    });

    shell.pack();
    shell.open();

    while (!shell.isDisposed())
    {
        if (!d.readAndDispatch())
        {
            d.sleep();
        }
    }
    d.dispose();
}

private static void hidePopUp()
{
    if (popup != null && !popup.isDisposed())
    {
        popup.close();
        popup = null;
    }
}

private static void callPopUp(Event e, Shell shell)
{
    System.out.println("CREATE");
    if (popup == null)
    {
        popup = new Shell(shell.getDisplay(), SWT.NO_TRIM | SWT.ON_TOP | SWT.MODELESS);
        popup.setLayout(new FillLayout());
        new Label(popup, SWT.NONE).setText("Howdy");
        popup.pack();
        popup.open();
        shell.forceFocus();
    }

    popup.setLocation(shell.getLocation().x + e.x, shell.getLocation().y + e.y);
}

Looks like this:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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