TreeTtem MouseHover上的SWT显示弹出窗口

时间:2014-03-04 09:25:43

标签: java swing swt awt

我试图在鼠标悬停在TreeItem元素上时显示一个弹出窗口(一开始很简单,但后面有按钮)。

这是我的代码:

   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();
                                }

                        }
                    });

不知何故,代码没有超过frame.add(jframe)。可能是什么原因?

干杯, 米哈伊

2 个答案:

答案 0 :(得分:2)

这是一个简单的例子:

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();
    }
}

enter image description here

答案 1 :(得分:1)

这是一个纯粹的SWT解决方案:

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);
}

看起来像这样:

enter image description here