Java向导条件下一页

时间:2015-10-23 10:19:35

标签: java wizard

我使用CJWizard创建了一个2页向导,其中第二页基于第一页的选择。

因此我总共有3页向导。

public class WizardTest extends JDialog
{

    public static void main(String[] args)
    {
        WizardTest test = new WizardTest();
        test.setVisible(true);
    }

    public WizardTest()
    {
        final WizardContainer wc
                = new WizardContainer(new TestFactory(),
                        new TitledPageTemplate(),
                        new StackWizardSettings());

        wc.addWizardListener(new WizardListener()
        {
            @Override
            public void onCanceled(List<WizardPage> path, WizardSettings settings)
            {
                WizardTest.this.dispose();
            }

            @Override
            public void onFinished(List<WizardPage> path, WizardSettings settings)
            {
                WizardTest.this.dispose();
            }

            @Override
            public void onPageChanged(WizardPage newPage, List<WizardPage> path)
            {
                WizardTest.this.setTitle(newPage.getDescription());
            }
        });

        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.getContentPane().add(wc);
        this.pack();
    }

    private class TestFactory extends APageFactory
    {

        private final WizardPage[] pages =
        {
            new WizardPage("One", "First Page")
            {
                {
                    String[] choices =
                    {
                        "Two", "Three"
                    };
                    JComboBox jComboBox = new JComboBox(new DefaultComboBoxModel(choices));
                    jComboBox.setName("nextPage");
                    add(new JLabel("Which Page Next?"));
                    add(jComboBox);
                }
            },
            new WizardPage("Two", "Second Page")
            {
                {
                    add(new JLabel("Two!"));
                    setBackground(Color.CYAN);
                }

                @Override
                public void rendering(List<WizardPage> path, WizardSettings settings)
                {
                    super.rendering(path, settings);
                    setFinishEnabled(true);
                    setNextEnabled(false);
                }

            },
            new WizardPage("Three", "Third Page")
            {
                {
                    add(new JLabel("Three!"));
                    setBackground(Color.MAGENTA);
                }

                @Override
                public void rendering(List<WizardPage> path, WizardSettings settings)
                {
                    super.rendering(path, settings);
                    setFinishEnabled(true);
                    setNextEnabled(false);
                }
            }
        };

        @Override
        public WizardPage createPage(List<WizardPage> path,
                WizardSettings settings)
        {
            WizardPage page;

            if (path.size() == 1)
            {
                String assignmentSelection = (String) settings.get("nextPage");
                if (assignmentSelection.equalsIgnoreCase("Three"))
                {
                    page = pages[path.size() + 1];
                }
                else
                {
                    page = pages[path.size()];
                }
            }
            else
            {
                page = pages[path.size()];
            }
            return page;
        }
    }
}

如果我从(选择的)第二页返回第一页并在第一页中更改选项再次调用createPage()并且已创建的页面是所示。

如何删除之前的选项并返回第一页而不显示原始选项?

1 个答案:

答案 0 :(得分:0)

每个页面都可以扩展WizardPage,因此您可以使用以下方法:getNextPage 并根据需要更改下一个wizardPage,它们可以通过您在构造函数中使用的名称进行标识... 条件页面流的示例:

public IWizardPage getNextPage() {
            // If they have complaints, go to the normal next page
            if (radioButtonRookies.getSelection()) {
                return super.getNextPage();
            }
            // No complaints? Short-circuit the rest of the pages
            return getWizard().getPage("generator");
        }