Sterls
Sterls

Reputation: 761

How to give focus to Eclipse RCP TitleAreaDialog?

I have a button in my Eclipse RCP View that makes a TitleAreaDialog popup with a form. However, when I click the button, the TitleAreaDialog pops up behind my view, rather than in front of it to catch the user's attention.

This is how my dialog is declared:

public class PopUpEditForm extends TitleAreaDialog {
    public PopUpEditForm(Shell parentShell) {
        super(parentShell);
        //Irrelevant logic
    }
//Other functions to populate/create the form
}

This is how it is called:

public void editFeature(){
    Display disp = new Display();
    Shell she = new Shell(disp);
    PopUpEditForm p = new PopUpEditForm(she);
}

I tried setting the focus (.setFocus()) for the container used to display the form, along with components of that container. I also used .forceActive() but to no avail. Halp.

EDIT: SOLUTION

I was using the wrong shell:

public void editFeature(){
    PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {

    public void run() {
            Shell activeShell =     PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
            PopUpEditForm p = new PopUpEditForm(activeShell);
    }        
}

Upvotes: 1

Views: 281

Answers (2)

Kumar
Kumar

Reputation: 192

I would suggest you use :

    public void editFeature()
    {
Display disp = new Display();
Shell she = new Shell(disp);
PopUpEditForm p = new PopUpEditForm(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
    }

Ideally dialog should be above window.

Upvotes: 1

flavio.donze
flavio.donze

Reputation: 8100

Try the following:

public void editFeature(){
    PopUpEditForm p = new PopUpEditForm(Display.getDefault().getActiveShell());
}

Since PopUpEditForm is a TitleAreaDialog which requires a parent shell, you should not have to create an new shell for it, but rather pass an existing shell (like the main application shell).

Since you are in an RCP view you might also do:

public void editFeature(){
    PopUpEditForm p = new PopUpEditForm(getSite().getShell());
}

Upvotes: 1

Related Questions