Vinayak Pahalwan
Vinayak Pahalwan

Reputation: 3005

Widgets doesn't show up while launching Eclipse RCP Application

I'm developing a eclipase based plugin where i've created an application (using SWT). There are two classes viz; RunAction.java which consists of run() , dispose() and init() methods and Sample.java which consists of the sample application with a Label widget. Now, when i test the application by Running it as an Eclipse application, onlt the shell is displayed without the label widget. What is the issue? I'm sharing the code.

RunAction.java

public class RunWizardAction extends Action implements IWorkbenchWindowActionDelegate {
    /** Called when the action is created. */ 
    Sample samp=new Sample();
    Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();


    public void init(IWorkbenchWindow window) {

    }

    /** Called when the action is discarded. */ 
    public void dispose() {
    }

    /** Called when the action is executed. */ 
    public void run(IAction action) {


        //InvokatronWizard wizard= new InvokatronWizard();
        new Thread(new Runnable(){
        @Override
        public void run() {

            samp.sampleApp(shell);

                    }
        }).start();
        }
}

Sample.java (The sample function)

public void sampleApp(Shell shell) {
              Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Hello");
        JLabel lab=new JLabel("Hello World");
        Label username_checkout=new Label(shell, SWT.BOLD);
        username_checkout.setText("User Name");
        Button button=new Button(shell,SWT.PUSH);
        button.setText("push");
        shell.open();
        shell.setSize(270,270);
        while (!shell1.isDisposed()) {
          if (!display.readAndDispatch()) {
            display.sleep();
          }
        }

    }

Upvotes: 0

Views: 206

Answers (2)

Baz
Baz

Reputation: 36884

Your Shell doesn't have a layout. This code works for me:

public static void main(String[] args)
{
    Display display = new Display();
    Shell shell = new Shell(display);

    /* SET LAYOUT */
    shell.setLayout(new FillLayout());
    shell.setText("Hello");
    JLabel lab = new JLabel("Hello World");
    Label username_checkout = new Label(shell, SWT.BOLD);
    username_checkout.setText("User Name");
    Button button = new Button(shell, SWT.PUSH);
    button.setText("push");
    shell.open();
    shell.pack();
    shell.setSize(270, 270);
    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
        {
            display.sleep();
        }
    }

}

Besides, there are too many Shells in your code:

public void sampleApp(Shell shell) {
    Display display = new Display();
    Shell shell = new Shell(display);

So, now you have two Shells called shell (which btw won't compile)...

while (!shell1.isDisposed())

And there is a third one. What's up with that?

Upvotes: 3

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51445

Your sample application would be fine if you were running outside of Eclipse like Baz's answer.

Since you're running inside of Eclipse, you already have a Display and a Shell. Use them.

public void sampleApp(Shell shell) { 
    shell.setLayout(new FillLayout());
    shell.setText("Hello");
    JLabel lab=new JLabel("Hello World");
    Label username_checkout=new Label(shell, SWT.BOLD);
    username_checkout.setText("User Name");
    Button button=new Button(shell,SWT.PUSH);
    button.setText("push");
    shell.setSize(270,270);
    shell.open();
} 

Upvotes: 3

Related Questions