I'm trying to write a program that has several navigation buttons, for example the classical "Back" and "Forward" buttons. I would need a way to let the user to navigate through the different pages (JPanel
s, to be true). How can I do that? Or better, how would a real programmer (I'm a beginner jet) do that? I'm using for the moment CardLayout
and I thought to store the "path" that the user does and use previous() and next() methods that CardLayout
provides. What about this solution? Is it a bit "dummy"? Does a kind of "navigation pattern" exist?
Asked
Active
Viewed 79 times
1

user1803551
- 12,965
- 5
- 47
- 74

user2896152
- 762
- 1
- 9
- 32
1 Answers
0
You may simply store created JPanels in a List or a Stack as long as they keep they internal states unchanged by any other JPanels. However this might be a little resources hungry, thats why its better to store just internal state of an object rather than whole object.
Take a look at Memento Pattern:

Praeterii
- 340
- 6
- 16
-
Thank you so much. I got that I could use Memento Pattern as logic to restore the state of the path, but from a point of view of the visualization, using always a CardLayout? Example, two JPanels, named panel_1 and panel_2. Initial state of the originator=1 and I show on my CardLayout panel_1. From panel_1, the user goes to panel_2, so the state is updated to 2 and panel_2 is shown on the screen. Is this the logic I should use??? – user2896152 May 08 '15 at 10:04
-
Take a closer look at Java Example from wikipedia. There is Originator which holds acctual state. I would make your JFrame an Originator. Originator.set() operation would be a setting acctual JPanel to be displayed in JFrame. Also add a List in JFrame similar to savedStates from Wikipedia to hold your memento (Memento would be all the data needed to recreate JPanel). Also hold some i which is a number of already displayed fragment. As for prev/next buittons simply get i-1/i+1 memento if exist, and recreate panel. You dont have to use CardLayout in this implementation – Praeterii May 08 '15 at 11:04
-
http://stackoverflow.com/questions/218155/how-do-i-change-jpanel-inside-a-jframe-on-the-fly – Praeterii May 08 '15 at 11:08