Eclipse: Getting null display
Asked Answered
B

4

5

I am trying to pop up a dialog (i.e. a FileDialog) in an Eclipse Plugin, actually before of an Acceleo transformation I am running through the related UI Launcher project (http://lowcoupling.com/post/51877317162/the-acceleo-ui-launcher-project)

I am trying to do this in the related doGenerate method...

public void doGenerate(IProgressMonitor monitor) throws IOException {

    Display display = Display.getCurrent();
    System.out.println(display);
    //....

but the display I get is null How should I do that?

Bebebebeerine answered 4/10, 2013 at 21:19 Comment(0)
P
5

The documentation of IWorkbench#getDisplay() states:

Code should always ask the workbench for the display rather than rely on Display.getDefault().

So use:

PlatformUI.getWorkbench().getDisplay()

instead.

Pozsony answered 4/10, 2013 at 21:36 Comment(2)
Note that this can't be used in a 'pure' e4 application where PlatformUI can't be used. For syncExec and asyncExec use org.eclipse.e4.ui.di.UISynchronize which can be injected.Chalcidice
@Chalcidice Good to know. Haven't used e4 yet.Pozsony
A
2

I'm not sure if you looked at this yet, but in the documentation for the class Display, it says that Display.getCurrent() returns

null if the currently running thread is not a user-interface thread for any display.

This might be the problem, but without more information I can't tell.

Aureomycin answered 4/10, 2013 at 21:34 Comment(1)
yes this is the problem but I still can't see where to show the dialogBebebebeerine
G
1

You can try getting the default display Display.getDefault() or the workbench display PlatformUI.getWorkbench().getDisplay()

If you are not running in the UI thread, try the following:

Display.getDefault().syncExec(new Runnable() {
    public void run() {
        // ... do work that updates the screen ...
    }
});
Gooding answered 4/10, 2013 at 21:39 Comment(1)
You are aware, that the second method you suggest states that using the first one is not recommended? ;)Pozsony
F
1

I was facing the same issue. I used getShell().getDisplay() instead of Display.getCurrent() and it worked for me.

Fuegian answered 25/6, 2015 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.