简体   繁体   中英

Is there a way to make a SWT shell moveable and on top?

I'm trying to make a dialog stay on top of it's parent. The following code is similar to what I've done with the child dialog, minus the passing in of a parent. I started out by writing the following code:

public static void main(String [] args)
{
    final Display display = new Display();
    final Shell shell = new Shell(display, SWT.ON_TOP);

    shell.setLayout(new FillLayout());
    shell.open();

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

This causes the dialog to remain on top, but now I'm no longer able to move it. I tried updating the call to the shell's constructor to:

final Shell shell = new Shell(display, SWT.ON_TOP | SWT.DIALOG_TRIM);

and

final Shell shell = new Shell(display, SWT.ON_TOP | SWT.SHELL_TRIM);

Both of these options allows me to change the size of the dialog by clicking and dragging on the border around the window but does not allow me to move the dialog.

The only thing I found online was to add a listener for mouse events and do the moving myself:

Listener l = new Listener()
{
    Point origin;
    @Override
    public void handleEvent(Event pEvent)
    {
        switch(pEvent.type)
        {
            case SWT.MouseDown:
                origin = new Point(pEvent.x, pEvent.y);
            break;
            case SWT.MouseUp:
                origin = null;
            break;
            case SWT.MouseMove:
                if(origin != null)
                {
                    Point p = display.map(shell, null, pEvent.x, pEvent.y);
                    shell.setLocation(p.x - origin.x, p.y - origin.y);
                }
            break;
        }
    }
};
shell.addListener(SWT.MouseDown, l);
shell.addListener(SWT.MouseUp, l);
shell.addListener(SWT.MouseMove, l);

shell.open(); //Rest of code as above

I found this suggestion at: http://jexp.ru/index.php/Java_Tutorial/SWT/Shell

Is there anyway to create a dialog in SWT that is always on top and has the same look, feel and interactions of a default SWT dialog (a dialog with style: SWT.SHELL_TRIM) without having to write my own listener?

You need use own listeners. Below code should help:-

  public class Demo {

        static Boolean blnMouseDown=false;
        static int xPos=0;
        static int yPos=0;

        public static void main(final String[] args) {
            Display display=new Display();
            final Shell shell = new Shell( Display.getDefault(), SWT.RESIZE); 
            shell.open();

            shell.addMouseListener(new MouseListener() {

                @Override
                public void mouseUp(MouseEvent arg0) {
                    // TODO Auto-generated method stub
                    blnMouseDown=false;
                }

                @Override
                public void mouseDown(MouseEvent e) {
                    // TODO Auto-generated method stub
                    blnMouseDown=true;
                    xPos=e.x;
                    yPos=e.y;
                }

                @Override
                public void mouseDoubleClick(MouseEvent arg0) {
                    // TODO Auto-generated method stub

                }
            });
            shell.addMouseMoveListener(new MouseMoveListener() {

                @Override
                public void mouseMove(MouseEvent e) {
                    // TODO Auto-generated method stub
                    if(blnMouseDown){

                        shell.setLocation(shell.getLocation().x+(e.x-xPos),shell.getLocation().y+(e.y-yPos));
                    }
                }
            });

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

    }

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