简体   繁体   中英

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();
          }
        }

    }

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 Shell s in your code:

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

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

while (!shell1.isDisposed())

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

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();
} 

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