简体   繁体   中英

Hide ON_TOP shell in SWT on Display minimimized

I'm trying to hide a SWT shell when the Display is minimized. I'm missing something and would be most thankful for any help.

Additional Info: This shell is actually a popup that gets drawn when the user clicks on a composite. In the end, my goal is to hide this popup-shell when the composite is not visible (user minimized the window or switched between windows, say with Alt+Tab for example).

Here's my code:

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

Here is sample code that will help you do figure out what you are looking for

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM