pankaj
pankaj

Reputation: 553

Rendering GUI of another class from calling its method in SWT?

I have two classes one is Helper . In Helper class I have some buttons and Text . I want use this class gui another class which is Widget. For this I have static method in Helper . I call this method from Widget class but some how I am not able to render Helper class gui in my Widget class . Help me if I am using some wrong term. Thanks in advance.

Here is my full code .

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class Helper extends Composite {

    public Helper(Composite parent, int style) {
        super(parent, style);
        initGUI(this);
    }

    private void initGUI(Composite parent) {
        parent.setLayout(new GridLayout(2, false));
        parent.setLayoutData(new GridData(GridData.FILL));

        Label label = new Label(parent, SWT.NONE);
        label.setText("label:");
        label.setLayoutData(new GridData(GridData.FILL_HORIZONTAL,GridData.FILL_VERTICAL,true,true,1,1));

        Text text = new Text(parent, SWT.SINGLE | SWT.BORDER);
        text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL,GridData.FILL_VERTICAL,true,true,1,1));

        Button button =  new Button(parent, SWT.PUSH);
        button.setText("hi I am ");
        button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL,GridData.FILL_VERTICAL,true,true,1,1));

        Button btn =  new Button(parent, SWT.PUSH);
        btn.setLayoutData(new GridData(GridData.FILL_HORIZONTAL,GridData.FILL_VERTICAL,true,true,1,1));
        btn.setText("hello");
    }

    public static Composite show(Shell shell){
        shell = new Shell();
        Helper wid = new Helper(shell, SWT.NONE);
        return wid;
    }

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

        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout());
        new Helper(shell, SWT.NONE);
        shell.open();
        shell.setText("Helper");
        shell.pack();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }
}


import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class Widget extends Composite {

    public Widget(Composite parent, int style) {
        super(parent, style);
        initGUI(this);
    }

    private void initGUI(Composite parent) {
        parent.setLayout(new GridLayout(3, false));
        parent.setLayoutData(new GridData());

        Text text = new Text(parent, SWT.SINGLE | SWT.BORDER);
        text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL,GridData.FILL_VERTICAL,true,true,1,1));

        Button button =  new Button(parent, SWT.PUSH);
        button.setText("not Helper");
        button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL,GridData.FILL_VERTICAL,true,true,1,1));

        Composite comp=Helper.show(parent.getShell());
        comp.setLayout(new GridLayout());
        comp.setLayoutData(new GridData());

    }

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

        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout());
        new Widget(shell, SWT.NONE);
        shell.open();
        shell.setText("New Widget");
        shell.pack();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }

}

Upvotes: 1

Views: 714

Answers (1)

Pradeep Kumar Barick
Pradeep Kumar Barick

Reputation: 58

You need to create object of Helper class because it extends composite so it behave like a container and you can adjust or set layout and layoutdata to helper class .

Upvotes: 2

Related Questions