如何在不关闭前一个shell的情况下打开弹出shell

时间:2014-09-22 12:42:24

标签: java shell swt eclipse-rcp jface

我正在使用这个PopupComposite,我想知道如何打开一个包含按钮的弹出shell(pc1),它打开另一个弹出shell(pc2),而不关闭第一个弹出shell。我尝试修改PopupComposite的激活监听器,但我得到的只是一个解决方案,每当我打开pc2时闪烁pc1。我在shellActivated中添加了以下代码:

if(shell.getParent() != null)
            shell.getParent().setVisible(true);

每当弹出窗口失去焦点时,他们必须隐藏(我不能将FocusListener用于shell,因为它不能在mac上工作)。

这是我的测试员:

public class TestShells
{
    public static void main(final String[] args)
    {
        final Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(1, false));



        final Composite container = new Composite(shell, SWT.NULL);
        container.setLayout(new FillLayout());

        final Button btn = new Button(container, SWT.PUSH);
        btn.setText("Button 1");

        final PopupComposite pc1 = new PopupComposite(Display.getDefault().getActiveShell(), SWT.NULL);
        final Button btn2 = new Button(pc1, SWT.PUSH);
        btn2.setText("Button 2");

        final PopupComposite pc2 = new PopupComposite(pc1.getShell(),SWT.NULL);
        final Text text = new Text(pc2, SWT.BORDER);

        btn.addSelectionListener(new SelectionListener()
        {

            public void widgetSelected(final SelectionEvent e)
            {
                pc1.show(btn.getLocation());
            }

            public void widgetDefaultSelected(final SelectionEvent e)
            {
            }
        });

        btn2.addSelectionListener(new SelectionListener()
        {

            public void widgetSelected(final SelectionEvent e)
            {
                pc2.show(btn2.getLocation());

            }

            public void widgetDefaultSelected(final SelectionEvent e)
            {
                // TODO Auto-generated method stub

            }
        });

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

非常感谢!

1 个答案:

答案 0 :(得分:1)

我不知道如何隐藏上一个窗口,但我作为一个Windows用户也可以告诉你,在一堆程序下面有一个弹出式消息通常会让人感到沮丧,所以会建议反对。

关于实际问题:你试过JDialog吗?只需在对话窗口中堆叠JOptionPane即可在按下按钮时显示。

请参阅http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

编辑:这里应该有效的代码片段,我还没有测试过。

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

public class Snippet99 {

  public static void main(String[] args) 
  {
    final private Display display = new Display();
    final private Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, false));

    final Composite container = new Composite(shell, SWT.NULL);
        container.setLayout(new FillLayout());

    final private Button btn = 
new Button(container, SWT.PUSH).setText("Button 1");
    final private PopupComposite pc1 = 
new  PopupComposite(Display.getDefault().getActiveShell(), SWT.NULL);
    final private Button btn2 = 
new Button(pc1, SWT.PUSH).setText("Button 2");
    final private PopupComposite pc2 = 

new PopupComposite(pc1.getShell(),SWT.NULL);

    private Text text = new Text(pc2, SWT.BORDER);
    /*
    only use final if you're setting the value immediately and will never change it again,
    make sure you set the accessibility of your items (protected/private/public)
    */

    Listener listener = new Listener() {
      public void handleEvent(Event event) {
        pc1.show(btn.getLocation());
      }
    };

    btn.addListener(SWT.Selection, listener);

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