[英]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. 我试图将鼠标悬停在TreeItem元素上显示一个弹出窗口(最初是简单的,但之后是按钮)。
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). 不知何故,代码不会超出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: 这是一个纯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);
}
Looks like this: 看起来像这样:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.