Java向导有条件,下一页



我使用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(),并且显示已经创建的页面。

我怎么能擦除以前的选择,回到第一页,而不总是显示原来的选择?

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

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");
        }

最新更新