Multiple Page application

I want to design an application that has dozens of pages. I could try and use CardLayout to accomplish this (although for the life of me I couldn't get CardLayout to work when I tried it on another application), however I would prefer not to have all of my pages in memory. Instead, I would prefer to use 1 Frame and create a JPanel for it. Then when I am done with that JPanel, I abandon it, and assign a new JPanel to show the next screen, abandoning that one as well when needed.

Here is the basic logic that I am using:

MainClass

//Create Frame

//Create starting JPanel and assign it to the frame

method ChangePanel(Parameter: JPanel)

//Get component count from the frame and remove existing JPanels from the frame

//Add new JPanel to the frame

StartingJPanelClass

method newpage_actionperformed

//Call MainClass.ChangePanel method (passing new JPanel)

The main concern that I have with this approach is that I don't know if the old pages are truly being abandoned, or if they are still existing in memory. In theory a user could go back and forth between these pages hundreds of times, and if the pages are not being recycled by the garbage collector, memory issues will be a big issue. The reason why I am not sure if they are being abandoned is that the old JPanel calls the MainClass which then removes it from the frame. The JPanel should no longer have a reference to it, however in debug mode, the system briefly returns it to the calling method. Since it has to execute some code after being abandoned, I fear that it is kept in memory.

I am also having a problem with my current implementation (that may or may not be related to it). When I go from Page One to Page Two back to Page One, I see a blank screen when I return to Page One. This is a new instance of Page One. Maybe this is due to the problem that I just described, or maybe it is unrelated.

Message was edited by:

fryet

[2003 byte] By [fryeta] at [2007-10-2 18:53:02]
# 1
You have to call dispose() on your GUI objects to get them cleaned up in most cases.BTW, what does this have to do with Java Game Development?
Herko_ter_Horsta at 2007-7-13 20:15:47 > top of Java-index,Other Topics,Java Game Development...
# 2
Thanks for the dispose tip. I will check it out. This multi-page application is actually a game, which is why I posted it here, however it could just as easily have been a business application.
fryeta at 2007-7-13 20:15:47 > top of Java-index,Other Topics,Java Game Development...
# 3
Dispose isn't working for me. It appears that dispose is used on JFrames, not JPanels. I don't want to destroy/recreate the frame each time the user decides to change the page. The frame has size and location information that I want to preserve, along with a menubar.
fryeta at 2007-7-13 20:15:47 > top of Java-index,Other Topics,Java Game Development...
# 4

I was able to work this out on my own. Here is what I found.

1: When it comes to my bug of returning back to the original page and seeing a blank frame, I was able to determine that the JVM did not consider the old page redone as "valid". Not sure why, however when I issued the command:

theFrame.setVisible(true);

The application was able to redraw the page (I was using a repaint command before this). Once this code fix was in, I was able to repeatedly switch back and forth between the 2 pages with no problems.

2: It was suggested to me that I use Task Manager in Windows to check the memory usage of my program. After repeatedly switching back and forth between the pages, I was able to confirm that I was not running into unnecessary JPanels stuck in memory.

Here is the code that I used to switch the JPanels associated with a frame:

public static void change_Panel(JPanel selectedPage)

{

theFrame.getContentPane().removeAll();

theFrame.getContentPane().add(BorderLayout.CENTER,selectedPage );

theFrame.setVisible(true);

}

Then when I wanted to switch pages, I would issue the following code as a result of the user clicking on a pushbutton:

Adventure.change_Panel(new StartScreen());

Note: theFrame is a JFrame. StartScreen is a class that extends JPanel. Adventure is the main class that contains the method change_Panel.

fryeta at 2007-7-13 20:15:47 > top of Java-index,Other Topics,Java Game Development...