简体   繁体   中英

How to avoid widgets being added while moving back and on then clicking Next?

I have created a Wizard.

it has 3 pages-

@Override
public void addPages() {
    super.addPages();
    addPage(firstPage);
    addPage(secondPage);   
    addPage(thirdPage);   
}

For the first page, I have a simple label with a input field to collect user entered value-

    @Override
    public void createControl(Composite parent) {
        Composite page = new Composite(parent, SWT.NONE);
        setControl(page);
        setPageComplete(false);

        // page.setLayout(new GridLayout(2, false));
        GridLayout gridLayout = new GridLayout();
        gridLayout.numColumns = 2;
        gridLayout.makeColumnsEqualWidth = false;
        page.setLayout(gridLayout);

        page.setLayoutData(new GridData(GridData.FILL_BOTH));

        Label label = new Label(page, SWT.NONE);
        label.setText("Interface Id : ");

        interfaceId = new Text(page, SWT.BORDER);
        GridData interfaceIdGridData = new GridData(GridData.FILL_HORIZONTAL);
        interfaceId.setLayoutData(interfaceIdGridData);



    }

在此处输入图片说明

When the user clicks on the Next button shown on the above wizard page, there is a call to collect some details from external system within getNextPage of the same wizard page.

The user then lands on wizard page 2 and the details which were fetched in step1 are displayed here in Wizard Page 2-

public class InterfaceDetailsPage extends WizardPage {

    private Composite top;
    private ScrolledComposite scrolledComposite;
    private Composite page;

    private Label responseCode;
    private Label responseMsg;
    private Label interfaceName;
    private Label sourceSystem;
    private Label targetSystem;
    private Label domainName;
    private Label serviceName;

    private SelectedServerNode iServer;
    private volatile boolean activityCancelled;
    private CreateSkeletonResponseModel model;
    private CreateSkeletonDisplayResultModel resultModel;

    protected InterfaceDetailsPage(SelectedServerNode iServer) {
        super("InterfaceDetailsPage");

        this.iServer = iServer;
        setTitle("Component Details");
        setMessage("The requested Interface details are shown below : ");
    }

    @Override
    public void createControl(Composite parent) {

        top = new Composite(parent, SWT.NONE);
        top.setLayout(new FillLayout());
        setControl(top);
        setPageComplete(false);


        scrolledComposite = new ScrolledComposite(top, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
        scrolledComposite.setExpandHorizontal(true);
        scrolledComposite.setExpandVertical(true);

        page = new Composite(scrolledComposite, SWT.NONE);
        //setControl(page);


        GridLayout gridLayout = new GridLayout();
        gridLayout.numColumns = 2;
        //gridLayout.makeColumnsEqualWidth = true;
        gridLayout.makeColumnsEqualWidth = false;
        page.setLayout(gridLayout);
        page.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));


        scrolledComposite.setContent(page);
        scrolledComposite.setSize(page.computeSize(SWT.DEFAULT, SWT.DEFAULT));


        GridData gridData = null;

        // Deals with the label response code and its value
        Label responseCodeLbl = new Label(page, SWT.NONE);
        //gridData = new GridData(GridData.FILL_HORIZONTAL);
        //responseCodeLbl.setLayoutData(gridData);
        responseCodeLbl.setText("Response Code : ");

        responseCode = new Label(page, SWT.NONE);
        gridData = new GridData(GridData.FILL_HORIZONTAL);
        responseCode.setLayoutData(gridData);

        // Deals with the label response message and its value
        Label responseMsgLbl = new Label(page, SWT.NONE);
        //gridData = new GridData(GridData.FILL_HORIZONTAL);
        //responseMsgLbl.setLayoutData(gridData);
        responseMsgLbl.setText("Response Message : ");

        responseMsg = new Label(page, SWT.NONE);
        gridData = new GridData(GridData.FILL_HORIZONTAL);
        responseMsg.setLayoutData(gridData);



    }

    @Override
    public void setVisible(boolean visible) {
        if (visible) {

            InterfacePage interfacePage = (InterfacePage) (getWizard().getPage("InterfacePage"));
            ModelObject model = interfacePage.getModelObject();

            GridData gridData = null;

            // Deals with the label interface name and its value
            Label interfaceNameLbl = new Label(page, SWT.NONE);
            //gridData = new GridData(GridData.FILL_HORIZONTAL);
            //interfaceNameLbl.setLayoutData(gridData);
            interfaceNameLbl.setText("Interface Id : ");

            interfaceName = new Label(page, SWT.NONE);
            gridData = new GridData(GridData.FILL_HORIZONTAL);
            interfaceName.setLayoutData(gridData);

            // Deals with the label source system and its value
            Label sourceSystemLbl = new Label(page, SWT.NONE);
            //gridData = new GridData(GridData.FILL_HORIZONTAL);
            //sourceSystemLbl.setLayoutData(gridData);
            sourceSystemLbl.setText("Source System : ");

            sourceSystem = new Label(page, SWT.NONE);
            gridData = new GridData(GridData.FILL_HORIZONTAL);
            sourceSystem.setLayoutData(gridData);

            // Deals with the label target system and its value
            Label targetSystemLbl = new Label(page, SWT.NONE);
            //gridData = new GridData(GridData.FILL_HORIZONTAL);
            //targetSystemLbl.setLayoutData(gridData);
            targetSystemLbl.setText("Target System : ");

            targetSystem = new Label(page, SWT.NONE);
            gridData = new GridData(GridData.FILL_HORIZONTAL);
            targetSystem.setLayoutData(gridData);

            // Deals with the label domain name and its value
            Label domainNameLbl = new Label(page, SWT.NONE);
            //gridData = new GridData(GridData.FILL_HORIZONTAL);
            //domainNameLbl.setLayoutData(gridData);
            domainNameLbl.setText("Domain Name : ");

            domainName = new Label(page, SWT.NONE);
            gridData = new GridData(GridData.FILL_HORIZONTAL);
            domainName.setLayoutData(gridData);

            // Deals with the label service name and its value
            Label serviceNameLbl = new Label(page, SWT.NONE);
            //gridData = new GridData(GridData.FILL_HORIZONTAL);
            //serviceNameLbl.setLayoutData(gridData);
            serviceNameLbl.setText("Service Name : ");

            serviceName = new Label(page, SWT.NONE);
            gridData = new GridData(GridData.FILL_HORIZONTAL);
            serviceName.setLayoutData(gridData);    




            interfaceName.setText(null != model.getInterfaceName() ? model.getInterfaceName() : "");
            // setTitle(model.getInterfaceID1() +" details");

            sourceSystem.setText(null != model.getSourceSystem() ? model.getSourceSystem() : "");
            targetSystem.setText(null != model.getTargetSystem() ? model.getTargetSystem() : "");
            domainName.setText(null != model.getDomainName() ? model.getDomainName() : "");
            serviceName.setText(null != model.getServiceName() ? model.getServiceName() : "");

            responseCode.setText(String.valueOf(model.getResponseCode()));
            responseMsg.setText(null != model.getResponseMessage() ? model.getResponseMessage() : "");

            page.layout();
            scrolledComposite.layout(true, true);
            scrolledComposite.setMinSize(page.computeSize(SWT.DEFAULT, SWT.DEFAULT));
            top.layout();
            setPageComplete(true);


        }
        super.setVisible(visible);
    } ...

在此处输入图片说明

As seen the last 5 labels displayed were generated inside the setVisible method

If now I move back by pressing the Back button and then click Next , I see the new labels are added under the previous rows and thus displayed twice. How can I prevent this behavior?

在此处输入图片说明

Creating controls in setVisible is wrong. You should only create controls in the page's createControl method.

The setVisible method should only set values in the controls created by createControls

If some controls are not always shown create them in createControls and call setVisible(false) to not display them.

If there are many variable values which are just displayed use a Table or TableViewer and set the contents in setVisible.

Finally if you really have to you can create things in setVisible but you will have to remember you have done this and not do it again on the second call to setVisible.

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