Question

I want to create a JFrame that switches between multiple JPanels, but has a more complex structure than the linked list that CardLayout seems to offer. Is there something that offers more of a directed graph based layout?

Here's the scenario I'm thinking of:

 ----------------------------  ->  ---------------
| Select User / New User Btn |    | New User Form |
 ----------------------------  <-  ---------------
     |         
     V
 --------
| Form 1 |
 --------
     |
     V
 --------       --------------
| Form 2 |  -> | Results Page |
 --------       --------------

As you can see this isn't a linked list, but a directed graph. Is there a standard way to deal with this scenario?

Was it helpful?

Solution 2

The CardLayout simply provides cards, it is up to you to determine which card should be visible. It provides some ordered-list style API methods for convenience (presumably because the designers decided that that was one of the more common use cases) but you don't have to use them.

Implement flow control in your buttons (or in whatever code you're using to determine the behavior here) and just use CardLayout.show() to show the appropriate card. You don't have to use first(), last(), previous(), and next() if you don't want to.

OTHER TIPS

I want to create a JFrame that switches between multiple JPanels, but has a more complex structure than the linked list that CardLayout seems to offer

You are not forced to view the panels in a CardLayout in linear order.

You can display any panel you wish by specifying the panel name you wish to display by using the show(...) method. Look at the example from the Swing tutorial on How to Use Card Layout for a working example that uses this approach.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top