This discussion is archived
1 Reply Latest reply: Dec 5, 2012 5:57 AM by jduprez RSS

what approch to follow to replace a jpanel with other jpanel

963723 Newbie
Currently Being Moderated
Hello Experts,

In my project i have a bunch of jpanels around 15 and a jframe now, at jframe in one point of time only one jpanel need to display and on some button event the other jpanel should display and replace the existing one and so on.........

now my confusion is how to achieve the following

1) do not load all jpanel in memory at once, just create the object of that jpanel only which is required and when other is required destroy the existing jpanel object so that it can free the memory and load the other jpanel into memory by creating object.

or what will be the best practice to make the application work faster with minimum and balanced cpu and ram utilization


some doubts:

a) is swing inbuild method jframe.remove() can solve it ?


Thanks............
  • 1. Re: what approch to follow to replace a jpanel with other jpanel
    jduprez Pro
    Currently Being Moderated
    Hello,
    At Swing level, having 10 panels "alive" in memory will cause zero CPU and memory problem. Period.
    So I suggest you look at the Swing tutorial lesson for CardLayout , whose job is exactly to display one panel at a time within a group of several: http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html

    Now, maybe having one of your panels, with all the non-graphical object graph it represents, implies some memory/CPU hog. Then I suggest to investigate that in your applicative object code. This will probably be investigated more simply on a sketch frame with only one panel; when you're done, try adding a second panel, switching with CardLayout, and investigate whether anything odd occurs; then you can light-heartedly go to 10 panels, and do validation and profiling.


    If you can already foresee, given your application's functionality (maybe each panel has to display a huge dataset fetched from DB), then you can start thinking about asynchronously load/discard data in your panel(s). That may or may not be quite involved if you're new with Swing, and you will certainly have to read the tutorial chapter on Swing and threads http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html (you will have to read it anyway if you do almost anything non-trivial in Swing).

    Best regards,

    J.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points