0

I have a bunch of panels put in a CardLayout where nth panel depends on the state changes caused in (n - 1)th panel. Since with CardLayout, you have to initialize and add all panels beforehand. As such it makes it harder than necessary to manage state.

Does Java or some third party open source library provide a variation of CardLayout that initializes its constituent panels lazily i.e. they are initialized just before they are going to be visible?

Edit:

Perhaps I did not state the problem clearly. Let me try again.

I need to set up the panels in CardLayout beforehand, but I do not want it to initialize them until they are to be made visible. This is necessary so that the state changes from previous stages are transparently propagated to next stages.

In my current code, I have:

cardsPanel.add(ReadMePanel.create(this), ReadMePanel.ID);
cardsPanel.add(LicencePanel.create(this), LicencePanel.ID);
cardsPanel.add(InstallationPathPanel.create(this), InstallationPathPanel.ID);
cardsPanel.add(
  ExtractionProgressPanel.create(
    this,
    new NormalizedPath(appContext.getParameter("zipFilePath")),
    new NormalizedPath(appContext.getInstallationDirectory().toString())
  ), 
  ExtractionProgressPanel.ID
);

Here InstallationPathPanel allows users to select a different installation directory than the default one. ExtractionProgressPanel is supposed to extract a certain zip file to this directory. if ExtractionProgressPanel were lazily initialized, the user selected path would be propagated to it, without me doing anything extra.

Hope the problem is clear now.

missingfaktor
  • 90,905
  • 62
  • 285
  • 365

1 Answers1

3

You can initialize and add the panel to the CardLayout right before calling the show method, so there is no problem.

EDIT

So on the location where you now call CardLayout#show, you could first do an add and then the show

As it looks like you are trying to create a wizard, the following article might be a good place to start

Robin
  • 36,233
  • 5
  • 47
  • 99
  • @missingfaktor I see your edit, and still do not see why you could not use this solution. – Robin Jun 05 '12 at 11:55
  • I never call `CardLayout#show`. I have a panel with `CardLayout` as its `LayoutManager`. – missingfaktor Jun 05 '12 at 11:56
  • Also the essence of question is reducing or if possible avoiding the bookkeeping I am having to do with the current approach. – missingfaktor Jun 05 '12 at 11:57
  • 1
    @missingfaktor Sounds like you want to create a wizard. Perhaps [this article](http://java.sun.com/developer/technicalArticles/GUI/swing/wizard/) is helpful – Robin Jun 05 '12 at 12:03
  • @mKorbel, I am not really up for that. Time constraints... sorry. – missingfaktor Jun 05 '12 at 12:05
  • The article proved to be quite useful. I refactored my code in accordance with the design described in the post, and that solved the problem I was facing earlier (the one described in my question.) If you include the link to this article in your answer, I will mark it as correct. – missingfaktor Jun 07 '12 at 14:38