Example usage for org.eclipse.swt.widgets Display asyncExec

List of usage examples for org.eclipse.swt.widgets Display asyncExec

Introduction

In this page you can find the example usage for org.eclipse.swt.widgets Display asyncExec.

Prototype

public void asyncExec(Runnable runnable) 

Source Link

Document

Causes the run() method of the runnable to be invoked by the user-interface thread at the next reasonable opportunity.

Usage

From source file:ProgressBarUpdateAnotherThread.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    final ProgressBar bar = new ProgressBar(shell, SWT.SMOOTH);
    bar.setBounds(10, 10, 200, 32);//w  w w .  ja v a2 s . c o  m
    shell.open();
    final int maximum = bar.getMaximum();
    new Thread() {
        public void run() {
            for (final int[] i = new int[1]; i[0] <= maximum; i[0]++) {
                try {
                    Thread.sleep(100);
                } catch (Throwable th) {
                }
                if (display.isDisposed())
                    return;
                display.asyncExec(new Runnable() {
                    public void run() {
                        if (bar.isDisposed())
                            return;
                        bar.setSelection(i[0]);
                    }
                });
            }
        }
    }.start();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet56.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 56");
    final ProgressBar bar = new ProgressBar(shell, SWT.SMOOTH);
    Rectangle clientArea = shell.getClientArea();
    bar.setBounds(clientArea.x, clientArea.y, 200, 32);
    shell.open();/*ww  w. j a v a  2  s  .c om*/
    final int maximum = bar.getMaximum();
    new Thread() {
        @Override
        public void run() {
            for (final int[] i = new int[1]; i[0] <= maximum; i[0]++) {
                try {
                    Thread.sleep(100);
                } catch (Throwable th) {
                }
                if (display.isDisposed())
                    return;
                display.asyncExec(() -> {
                    if (bar.isDisposed())
                        return;
                    bar.setSelection(i[0]);
                });
            }
        }
    }.start();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:Snippet104.java

public static void main(String[] args) {
    final Display display = new Display();
    final int[] count = new int[] { 4 };
    final Image image = new Image(display, 300, 300);
    final Shell splash = new Shell(SWT.ON_TOP);
    final ProgressBar bar = new ProgressBar(splash, SWT.NONE);
    bar.setMaximum(count[0]);//ww w .  j ava2  s.  c  om
    Label label = new Label(splash, SWT.NONE);
    label.setImage(image);
    FormLayout layout = new FormLayout();
    splash.setLayout(layout);
    FormData labelData = new FormData();
    labelData.right = new FormAttachment(100, 0);
    labelData.bottom = new FormAttachment(100, 0);
    label.setLayoutData(labelData);
    FormData progressData = new FormData();
    progressData.left = new FormAttachment(0, 5);
    progressData.right = new FormAttachment(100, -5);
    progressData.bottom = new FormAttachment(100, -5);
    bar.setLayoutData(progressData);
    splash.pack();
    Rectangle splashRect = splash.getBounds();
    Rectangle displayRect = display.getBounds();
    int x = (displayRect.width - splashRect.width) / 2;
    int y = (displayRect.height - splashRect.height) / 2;
    splash.setLocation(x, y);
    splash.open();
    display.asyncExec(new Runnable() {
        public void run() {
            Shell[] shells = new Shell[count[0]];
            for (int i = 0; i < count[0]; i++) {
                shells[i] = new Shell(display);
                shells[i].setSize(300, 300);
                shells[i].addListener(SWT.Close, new Listener() {
                    public void handleEvent(Event e) {
                        --count[0];
                    }
                });
                bar.setSelection(i + 1);
                try {
                    Thread.sleep(1000);
                } catch (Throwable e) {
                }
            }
            splash.close();
            image.dispose();
            for (int i = 0; i < count[0]; i++) {
                shells[i].open();
            }
        }
    });
    while (count[0] != 0) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet104.java

public static void main(String[] args) {
    final Display display = new Display();
    final int[] count = new int[] { 4 };
    final Image image = new Image(display, 300, 300);
    GC gc = new GC(image);
    gc.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
    gc.fillRectangle(image.getBounds());
    gc.drawText("Splash Screen", 10, 10);
    gc.dispose();/* w w  w  .  j  a va  2 s. co  m*/
    final Shell splash = new Shell(SWT.ON_TOP);
    final ProgressBar bar = new ProgressBar(splash, SWT.NONE);
    bar.setMaximum(count[0]);
    Label label = new Label(splash, SWT.NONE);
    label.setImage(image);
    FormLayout layout = new FormLayout();
    splash.setLayout(layout);
    FormData labelData = new FormData();
    labelData.right = new FormAttachment(100, 0);
    labelData.bottom = new FormAttachment(100, 0);
    label.setLayoutData(labelData);
    FormData progressData = new FormData();
    progressData.left = new FormAttachment(0, 5);
    progressData.right = new FormAttachment(100, -5);
    progressData.bottom = new FormAttachment(100, -5);
    bar.setLayoutData(progressData);
    splash.pack();
    Rectangle splashRect = splash.getBounds();
    Rectangle displayRect = display.getBounds();
    int x = (displayRect.width - splashRect.width) / 2;
    int y = (displayRect.height - splashRect.height) / 2;
    splash.setLocation(x, y);
    splash.open();
    display.asyncExec(() -> {
        Shell[] shells = new Shell[count[0]];
        for (int i1 = 0; i1 < count[0]; i1++) {
            shells[i1] = new Shell(display);
            shells[i1].setSize(300, 300);
            shells[i1].addListener(SWT.Close, e -> --count[0]);
            bar.setSelection(i1 + 1);
            try {
                Thread.sleep(1000);
            } catch (Throwable e) {
            }
        }
        splash.close();
        image.dispose();
        for (int i2 = 0; i2 < count[0]; i2++) {
            shells[i2].setText("SWT Snippet 104 - " + (i2 + 1));
            shells[i2].open();
        }
    });
    while (count[0] != 0) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:SplashScreenCreate.java

public static void main(String[] args) {
    final Display display = new Display();
    final int[] count = new int[] { 4 };
    final Image image = new Image(display, 300, 300);
    GC gc = new GC(image);
    gc.setBackground(display.getSystemColor(SWT.COLOR_CYAN));
    gc.fillRectangle(image.getBounds());
    gc.drawText("Splash Screen", 10, 10);
    gc.dispose();/*from  w  ww.j av  a  2s .  c  o m*/
    final Shell splash = new Shell(SWT.ON_TOP);
    final ProgressBar bar = new ProgressBar(splash, SWT.NONE);
    bar.setMaximum(count[0]);
    Label label = new Label(splash, SWT.NONE);
    label.setImage(image);
    FormLayout layout = new FormLayout();
    splash.setLayout(layout);
    FormData labelData = new FormData();
    labelData.right = new FormAttachment(100, 0);
    labelData.bottom = new FormAttachment(100, 0);
    label.setLayoutData(labelData);
    FormData progressData = new FormData();
    progressData.left = new FormAttachment(0, 5);
    progressData.right = new FormAttachment(100, -5);
    progressData.bottom = new FormAttachment(100, -5);
    bar.setLayoutData(progressData);
    splash.pack();
    Rectangle splashRect = splash.getBounds();
    Rectangle displayRect = display.getBounds();
    int x = (displayRect.width - splashRect.width) / 2;
    int y = (displayRect.height - splashRect.height) / 2;
    splash.setLocation(x, y);
    splash.open();
    display.asyncExec(new Runnable() {
        public void run() {
            Shell[] shells = new Shell[count[0]];
            for (int i = 0; i < count[0]; i++) {
                shells[i] = new Shell(display);
                shells[i].setSize(300, 300);
                shells[i].addListener(SWT.Close, new Listener() {
                    public void handleEvent(Event e) {
                        --count[0];
                    }
                });
                bar.setSelection(i + 1);
                try {
                    Thread.sleep(1000);
                } catch (Throwable e) {
                }
            }
            splash.close();
            image.dispose();
            for (int i = 0; i < count[0]; i++) {
                shells[i].open();
            }
        }
    });
    while (count[0] != 0) {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

From source file:org.eclipse.swt.snippets.Snippet343.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Snippet 343");
    shell.setLayout(new FillLayout());
    shell.setSize(400, 400);//w  ww. j  a v  a2s. co  m
    SashForm sashForm = new SashForm(shell, SWT.HORIZONTAL);
    Composite leftComposite = new Composite(sashForm, SWT.NONE);
    leftComposite.setLayout(new FillLayout());
    Composite rightComposite = new Composite(sashForm, SWT.NONE);
    rightComposite.setLayout(new FillLayout());
    ExpandBar expandBar = new ExpandBar(leftComposite, SWT.NONE);
    final ExpandItem expandItem1 = new ExpandItem(expandBar, SWT.NONE);
    expandItem1.setText("item 1");
    new ExpandItem(expandBar, SWT.NONE).setText("item 2"); /* expandItem2 */

    final StyledText text = new StyledText(expandBar, SWT.MULTI | SWT.WRAP);
    expandItem1.setControl(text);
    text.setText("initial text that will wrap if it's long enough");

    /* update the item's height if needed in response to changes in the text's size */
    final int TRIAL_WIDTH = 100;
    final int trimWidth = text.computeTrim(0, 0, TRIAL_WIDTH, 100).width - TRIAL_WIDTH;
    text.addListener(SWT.Modify, event -> {
        Point size = text.computeSize(text.getSize().x - trimWidth, SWT.DEFAULT);
        if (expandItem1.getHeight() != size.y) {
            expandItem1.setHeight(size.y);
        }
    });
    expandBar.addListener(SWT.Resize, event -> display.asyncExec(() -> {
        /*
         * The following is done asynchronously to allow the Text's width
         * to be changed before re-calculating its preferred height.
         */
        if (text.isDisposed())
            return;
        Point size = text.computeSize(text.getSize().x - trimWidth, SWT.DEFAULT);
        if (expandItem1.getHeight() != size.y) {
            expandItem1.setHeight(size.y);
        }
    }));

    shell.open();
    /* set the item's initial height */
    Point size = text.computeSize(expandBar.getClientArea().width, SWT.DEFAULT);
    expandItem1.setHeight(size.y);
    expandItem1.setExpanded(true);

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

From source file:org.eclipse.swt.snippets.Snippet209.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    Composite comp = new Composite(shell, SWT.NONE);
    comp.setLayout(new FillLayout());
    GLData data = new GLData();
    data.doubleBuffer = true;/*from  w w  w.ja  v  a2 s  . c o m*/
    final GLCanvas canvas = new GLCanvas(comp, SWT.NONE, data);

    canvas.setCurrent();
    final GLContext context = GLDrawableFactory.getFactory().createExternalGLContext();

    canvas.addListener(SWT.Resize, new Listener() {
        public void handleEvent(Event event) {
            Rectangle bounds = canvas.getBounds();
            float fAspect = (float) bounds.width / (float) bounds.height;
            canvas.setCurrent();
            context.makeCurrent();
            GL gl = context.getGL();
            gl.glViewport(0, 0, bounds.width, bounds.height);
            gl.glMatrixMode(GL.GL_PROJECTION);
            gl.glLoadIdentity();
            GLU glu = new GLU();
            glu.gluPerspective(45.0f, fAspect, 0.5f, 400.0f);
            gl.glMatrixMode(GL.GL_MODELVIEW);
            gl.glLoadIdentity();
            context.release();
        }
    });

    context.makeCurrent();
    GL gl = context.getGL();
    gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    gl.glColor3f(1.0f, 0.0f, 0.0f);
    gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
    gl.glClearDepth(1.0);
    gl.glLineWidth(2);
    gl.glEnable(GL.GL_DEPTH_TEST);
    context.release();

    shell.setText("SWT/JOGL Example");
    shell.setSize(640, 480);
    shell.open();

    display.asyncExec(new Runnable() {
        int rot = 0;

        public void run() {
            if (!canvas.isDisposed()) {
                canvas.setCurrent();
                context.makeCurrent();
                GL gl = context.getGL();
                gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
                gl.glClearColor(.3f, .5f, .8f, 1.0f);
                gl.glLoadIdentity();
                gl.glTranslatef(0.0f, 0.0f, -10.0f);
                float frot = rot;
                gl.glRotatef(0.15f * rot, 2.0f * frot, 10.0f * frot, 1.0f);
                gl.glRotatef(0.3f * rot, 3.0f * frot, 1.0f * frot, 1.0f);
                rot++;
                gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE);
                gl.glColor3f(0.9f, 0.9f, 0.9f);
                drawTorus(gl, 1, 1.9f + ((float) Math.sin((0.004f * frot))), 15, 15);
                canvas.swapBuffers();
                context.release();
                display.asyncExec(this);
            }
        }
    });

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

From source file:org.eclipse.swt.snippets.Snippet195.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    Composite comp = new Composite(shell, SWT.NONE);
    comp.setLayout(new FillLayout());
    GLData data = new GLData();
    data.doubleBuffer = true;/*  w w w  .j  a v  a2 s  .c o m*/
    final GLCanvas canvas = new GLCanvas(comp, SWT.NONE, data);

    canvas.setCurrent();
    try {
        GLContext.useContext(canvas);
    } catch (LWJGLException e) {
        e.printStackTrace();
    }

    canvas.addListener(SWT.Resize, new Listener() {
        public void handleEvent(Event event) {
            Rectangle bounds = canvas.getBounds();
            float fAspect = (float) bounds.width / (float) bounds.height;
            canvas.setCurrent();
            try {
                GLContext.useContext(canvas);
            } catch (LWJGLException e) {
                e.printStackTrace();
            }
            GL11.glViewport(0, 0, bounds.width, bounds.height);
            GL11.glMatrixMode(GL11.GL_PROJECTION);
            GL11.glLoadIdentity();
            GLU.gluPerspective(45.0f, fAspect, 0.5f, 400.0f);
            GL11.glMatrixMode(GL11.GL_MODELVIEW);
            GL11.glLoadIdentity();
        }
    });

    GL11.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    GL11.glColor3f(1.0f, 0.0f, 0.0f);
    GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
    GL11.glClearDepth(1.0);
    GL11.glLineWidth(2);
    GL11.glEnable(GL11.GL_DEPTH_TEST);

    shell.setText("SWT/LWJGL Example");
    shell.setSize(640, 480);
    shell.open();

    display.asyncExec(new Runnable() {
        int rot = 0;

        public void run() {
            if (!canvas.isDisposed()) {
                canvas.setCurrent();
                try {
                    GLContext.useContext(canvas);
                } catch (LWJGLException e) {
                    e.printStackTrace();
                }
                GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
                GL11.glClearColor(.3f, .5f, .8f, 1.0f);
                GL11.glLoadIdentity();
                GL11.glTranslatef(0.0f, 0.0f, -10.0f);
                float frot = rot;
                GL11.glRotatef(0.15f * rot, 2.0f * frot, 10.0f * frot, 1.0f);
                GL11.glRotatef(0.3f * rot, 3.0f * frot, 1.0f * frot, 1.0f);
                rot++;
                GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE);
                GL11.glColor3f(0.9f, 0.9f, 0.9f);
                drawTorus(1, 1.9f + ((float) Math.sin((0.004f * frot))), 15, 15);
                canvas.swapBuffers();
                display.asyncExec(this);
            }
        }
    });

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

From source file:org.eclipse.swt.snippets.Snippet209.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    Composite comp = new Composite(shell, SWT.NONE);
    comp.setLayout(new FillLayout());
    GLData data = new GLData();
    data.doubleBuffer = true;//  w ww  .  j  a  va2 s.  c  o  m
    final GLCanvas canvas = new GLCanvas(comp, SWT.NONE, data);

    canvas.setCurrent();
    final GLContext context = GLDrawableFactory.getFactory(GLProfile.getGL2GL3()).createExternalGLContext();
    ;

    canvas.addListener(SWT.Resize, new Listener() {
        @Override
        public void handleEvent(Event event) {
            Rectangle bounds = canvas.getBounds();
            float fAspect = (float) bounds.width / (float) bounds.height;
            canvas.setCurrent();
            context.makeCurrent();
            GL2 gl = (GL2) context.getGL();
            gl.glViewport(0, 0, bounds.width, bounds.height);
            gl.glMatrixMode(GL2.GL_PROJECTION);
            gl.glLoadIdentity();
            GLU glu = new GLU();
            glu.gluPerspective(45.0f, fAspect, 0.5f, 400.0f);
            gl.glMatrixMode(GL2.GL_MODELVIEW);
            gl.glLoadIdentity();
            context.release();
        }
    });

    context.makeCurrent();
    GL2 gl = (GL2) context.getGL();
    gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    gl.glColor3f(1.0f, 0.0f, 0.0f);
    gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
    gl.glClearDepth(1.0);
    gl.glLineWidth(2);
    gl.glEnable(GL.GL_DEPTH_TEST);
    context.release();

    shell.setText("SWT/JOGL Example");
    shell.setSize(640, 480);
    shell.open();

    display.asyncExec(new Runnable() {
        int rot = 0;

        @Override
        public void run() {
            if (!canvas.isDisposed()) {
                canvas.setCurrent();
                context.makeCurrent();
                GL2 gl = (GL2) context.getGL();
                gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
                gl.glClearColor(.3f, .5f, .8f, 1.0f);
                gl.glLoadIdentity();
                gl.glTranslatef(0.0f, 0.0f, -10.0f);
                float frot = rot;
                gl.glRotatef(0.15f * rot, 2.0f * frot, 10.0f * frot, 1.0f);
                gl.glRotatef(0.3f * rot, 3.0f * frot, 1.0f * frot, 1.0f);
                rot++;
                gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL2.GL_LINE);
                gl.glColor3f(0.9f, 0.9f, 0.9f);
                drawTorus(gl, 1, 1.9f + ((float) Math.sin((0.004f * frot))), 15, 15);
                canvas.swapBuffers();
                context.release();
                display.asyncExec(this);
            }
        }
    });

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

From source file:org.eclipse.swt.snippets.Snippet341.java

public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FormLayout());
    GLData data = new GLData();
    data.doubleBuffer = true;/*w  ww  .  j a  v  a2 s.c om*/
    final GLCanvas canvas = new GLCanvas(shell, SWT.NONE, data);

    canvas.setCurrent();
    GL.createCapabilities();

    canvas.addListener(SWT.Resize, event -> {
        Rectangle bounds = canvas.getBounds();
        float fAspect = (float) bounds.width / (float) bounds.height;
        canvas.setCurrent();
        GL.createCapabilities();
        GL11.glViewport(0, 0, bounds.width, bounds.height);
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
        float near = 0.5f;
        float bottom = -near * (float) Math.tan(45.f / 2);
        float left = fAspect * bottom;
        GL11.glFrustum(left, -left, bottom, -bottom, near, 400.f);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glLoadIdentity();
    });

    GL11.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    GL11.glColor3f(1.0f, 0.0f, 0.0f);
    GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
    GL11.glClearDepth(1.0);
    GL11.glLineWidth(2);
    GL11.glEnable(GL11.GL_DEPTH_TEST);

    Button button = new Button(shell, SWT.PUSH);
    button.setText("Capture");
    button.addListener(SWT.Selection, event -> capture(canvas));

    FormData formData = new FormData(640, 480);
    formData.top = new FormAttachment(0, 0);
    formData.left = new FormAttachment(0, 0);
    formData.bottom = new FormAttachment(button, 0);
    formData.right = new FormAttachment(100, 0);
    canvas.setLayoutData(formData);
    formData = new FormData();
    formData.left = new FormAttachment(0, 0);
    formData.bottom = new FormAttachment(100, 0);
    formData.right = new FormAttachment(100, 0);
    button.setLayoutData(formData);

    shell.pack();
    shell.open();

    display.asyncExec(new Runnable() {
        int rot = 0;

        @Override
        public void run() {
            if (!canvas.isDisposed()) {
                canvas.setCurrent();
                GL.createCapabilities();
                GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
                GL11.glClearColor(.3f, .5f, .8f, 1.0f);
                GL11.glLoadIdentity();
                GL11.glTranslatef(0.0f, 0.0f, -10.0f);
                float frot = rot;
                GL11.glRotatef(0.15f * rot, 2.0f * frot, 10.0f * frot, 1.0f);
                GL11.glRotatef(0.3f * rot, 3.0f * frot, 1.0f * frot, 1.0f);
                rot++;
                GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE);
                GL11.glColor3f(0.9f, 0.9f, 0.9f);
                drawTorus(1, 1.9f + ((float) Math.sin((0.004f * frot))), 15, 15);
                canvas.swapBuffers();
                display.asyncExec(this);
            }
        }
    });

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