如何避免在返回和单击下一步时添加小部件?

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

我创建了一个向导。

它有 3 页-

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

对于第一页,我有一个简单的 label 和一个 input 字段来收集用户输入的值-

    @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);



    }

当用户单击上述向导页面上显示的“下一步”按钮时,在同一向导页面的 getNextPage 中会调用从外部系统收集一些详细信息。

用户然后登陆向导页面 2,在 step1 中获取的详细信息显示在向导页面 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);
    } ...

正如所见,显示的最后 5 个标签是在 setVisible 方法中生成的

如果我现在按 Back 按钮后退,然后单击 Next,我会看到新标签已添加到前几行的下方,因此会显示两次。我怎样才能防止这种行为?

setVisible 中创建控件是错误的。您应该只在页面的 createControl 方法中创建控件。

setVisible 方法只能在 createControls

创建的控件中设置值

如果某些控件并不总是显示,请在 createControls 中创建它们并调用 setVisible(false) 以不显示它们。

如果刚刚显示的变量值很多,请使用Table或TableViewer并在setVisible中设置内容。

最后,如果您确实需要,您可以在 setVisible 中创建内容,但您必须记住您已经完成了此操作,并且在第二次调用 setVisible 时不要再次执行此操作。