Frage

I just started using eclipse window builder. I want to switch between two composites when I click a button. This is what I have in my main window :

public class DashboardView {

protected Shell shell;
private Composite composite;
private Next next;
private FormLayout layout;

public static void main(String[] args) {
    try {
        DashboardView window = new DashboardView();
        window.open();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void open() {
    Display display = Display.getDefault();
    createContents();
    shell.open();
    shell.layout();
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
}

protected void createContents() {
    shell = new Shell();
    shell.setSize(572, 390);
    shell.setText("SWT Application");
    layout = new FormLayout();
    shell.setLayout(layout);

    composite = new Composite(shell, SWT.NONE);
    composite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
    FormData fd_composite_1 = new FormData();
    fd_composite_1.top = new FormAttachment(0, 10);
    fd_composite_1.left = new FormAttachment(0, 10);
    fd_composite_1.bottom = new FormAttachment(0, 299);
    fd_composite_1.right = new FormAttachment(0, 546);
    composite.setLayoutData(fd_composite_1);

    Button btnNewButton = new Button(shell, SWT.NONE);
    btnNewButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            composite.dispose();
            next = new Next(shell, SWT.NONE);
        }
    });
    FormData fd_btnNewButton = new FormData();
    fd_btnNewButton.bottom = new FormAttachment(100, -10);
    fd_btnNewButton.right = new FormAttachment(composite, 0, SWT.RIGHT);
    btnNewButton.setLayoutData(fd_btnNewButton);
    btnNewButton.setText("New Button");

}
}

The composite I am trying to change to is the one called 'Next' in the above code. But when I click on the button, the current composite gets removed because of the dispose function but the Next composite isnt seen. In fact nothing is there at all except the button. Since its not a part of the composite.

This is the 'Next' composite :

public class Next extends Composite {
private Text txtYouAreIn;
public Next(Composite parent, int style) {
    super(parent, style);

    Button btnYay = new Button(this, SWT.NONE);
    btnYay.setBounds(43, 58, 75, 25);
    btnYay.setText("Yay");

    txtYouAreIn = new Text(this, SWT.BORDER);
    txtYouAreIn.setText("You are in a new");
    txtYouAreIn.setBounds(173, 113, 115, 21);

}
}

Am I doing something wrong? What should I do to change it?

War es hilfreich?

Lösung

Change the Next class as follows:

 public class Next extends Composite {
     private Text txtYouAreIn;
     public Next(Composite parent, int style) {
        super(parent, style);
         Button btnYay = new Button(parent, SWT.NONE);
         btnYay.setBounds(43, 58, 75, 25);
         btnYay.setText("Yay");

         txtYouAreIn = new Text(parent, SWT.BORDER);
         txtYouAreIn.setText("You are in a new");
         txtYouAreIn.setBounds(173, 113, 115, 21);
     }
 }

Add the below line in btnNewButton listener i.e in widgetSelected method

 composite.setVisible(!composite.isVisible());
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top