在显示最小化的SWT中隐藏ON_TOP shell

时间:2014-03-13 14:58:33

标签: java swt

当显示最小化时,我正在尝试隐藏SWT shell。我错过了一些东西,非常感谢任何帮助。

附加信息:此shell实际上是一个弹出窗口,当用户单击复合时会绘制该弹出窗口。最后,我的目标是在复合体不可见时隐藏此弹出窗口(用户最小化窗口或在窗口之间切换,例如使用Alt + Tab)。

这是我的代码:

static Shell middleClickNodeInfoShell ;
static Label nodeIdLabel ;

void init(){
    ... 
    /** Focused node on middle click*/
    middleClickNodeInfoShell = new Shell(Display.getDefault(), SWT.BORDER | SWT.MODELESS);
    middleClickNodeInfoShell.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, true, false));
    middleClickNodeInfoShell.setLayout(createNoMarginLayout(1, false));

    nodeIdLabel = new Label(middleClickNodeInfoShell, SWT.NONE);
    Display.getDefault().addListener(SWT.Iconify,new Listener() {
        @Override
        public void handleEvent(Event arg0) {
            // TODO Auto-generated method stub
            middleClickNodeInfoShell.setVisible(false);
        }
    });
}   

@Override
public boolean onMouseClicked(Button button, ScreenPosition screenPos,
            final GeoPosition arg2) {
    ... 

    nodeIdLabel.setText("Node Id: "+node.getId());
    middleClickNodeInfoShell.setLocation(pos.getX()+displayX,pos.getY()+displayY+30);
    middleClickNodeInfoShell.setVisible(true);
    middleClickNodeInfoShell.pack();
}

1 个答案:

答案 0 :(得分:1)

以下示例代码可帮助您找出所需内容

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setSize(300, 200);
    shell.setText("Shell Example");
    shell.setLayout(new RowLayout());

    final Button button = new Button(shell, SWT.PUSH);
    button.setText("Click Me");


    final Shell tip = new Shell(shell,SWT.MODELESS);
    tip.setLayout(new FillLayout());
    Label lbl = new Label(tip, SWT.NONE);
    lbl.setText("***tooltip***");
    tip.pack();
    shell.addControlListener(new ControlListener() {
      @Override
      public void controlResized(ControlEvent e) {
        changeTipLocation(display, button, tip);
      }
      @Override
      public void controlMoved(ControlEvent e) {
        changeTipLocation(display, button, tip);
      }
    });

    button.addSelectionListener(new SelectionAdapter() {

      public void widgetSelected(SelectionEvent event) {
        changeTipLocation(display, button, tip);
        tip.open();
      }

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

  }

  private static void changeTipLocation(final Display display, final Button button, final Shell tip) {

    Rectangle bounds = button.getBounds();
    Point loc = button.getLocation();
    tip.setLocation(display.map(button, null, new Point(loc.x+bounds.width, loc.y+bounds.height)));
  }