3

所以我偷了这个很酷PopupComposite的,我真的很满意。

只有一个问题。如果它把 a 放进org.eclipse.swt.widgets.Text去,我打开弹出窗口,聚焦Text,然后按ESC,然后两者都Text自行PopupComposite处理。

我真的无法弄清楚 dispose 调用来自哪里。这是一个Shell问题吗?我Shell应该在弹出窗口中使用什么?

SSCCE

/**
 * 
 * @author ggrec
 *
 */
public class PopupCompositeTester
{

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

    private PopupCompositeTester()
    {
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(1, false));

        createContents(shell);

        shell.pack();
        shell.open();
        while (!shell.isDisposed())
        {
            if ( !display.readAndDispatch() )
                display.sleep();
        }
        display.dispose();
    }

    private static void createContents(final Composite parent)
    {
        final Button button = new Button(parent, SWT.PUSH);
        button.setText("Poke Me");

        final PopupComposite popup = new PopupComposite(parent.getShell());
        new Text(popup, SWT.NONE);
        popup.pack();

        button.addSelectionListener(new SelectionAdapter()
        {
            @Override public void widgetSelected(final SelectionEvent e)
            {
                popup.show( Display.getDefault().map(parent, null, button.getLocation()) );
            }
        });
    }
}
4

2 回答 2

2

这样做的原因是,当您聚焦文本字段并按 Escape 时,该字段会向SWT.TRAVERSE_ESCAPE其父 shell 发送一个事件。外壳(在您的情况下不是顶级外壳)通过调用响应Shell.close()。您可以通过向文本字段添加遍历侦听器来解决此问题,这将取消事件(下面的代码)。

new Text(popup, SWT.NONE).addTraverseListener(new TraverseListener() {
    @Override
    public void keyTraversed(TraverseEvent e) {
        if(e.detail == SWT.TRAVERSE_ESCAPE) {
            e.doit = false;
        }
    }
});

请记住,这是针对您的特定问题的相当粗略的解决方案。除了测试目的,我不建议将其用于任何其他目的。您可以在这里阅读更多相关信息 -> http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fswt%2Fevents% 2FTraverseEvent.html

在这里:http ://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fswt%2Fwidgets%2FShell.html

于 2013-09-12T14:00:43.677 回答
0

因为我的“错误”实际上是 SWT 平台的正常行为,所以我使用了以下解决方法:

/**
 * Lazy initialization of the popup composite
 */
private void createPopup()
{
     // popupContainer is now a field
     if (popupContainer != null && !popupContainer.isDisposed())
         return;

     // ... create popup AND its contents ...
}

在按钮监听器中:

createPopup();
popup.show( Display.getDefault().map(parent, null, button.getLocation()) );


谢谢@blgt

于 2013-09-12T14:53:14.283 回答