Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   How to return to an instance? (http://www.programmingforums.org/showthread.php?t=15541)

namsu Apr 2nd, 2008 10:12 AM

How to return to an instance?
 
I am having a problem. If I have a JFrame open in a class and it performing and processing, when I close it, it still performs the work, which is good because this is what I want it to do. How do I actually return to that instance of the running program, if I go back to the main menu and click the button to load it up again, it makes a brand new instance. I need to return to the already running one.

Code for the back button

MainMenu m = new MainMenu();
m.setVisible(true);
dispose();

Code for loading up JFRAME Again from Main Menu

SandHopper sHopper = new SandHopper();
dispose();
sHopper.setVisible(true);

If someone can help me out here it would be great. Thank you.

Jimbo Apr 2nd, 2008 11:17 AM

Re: How to return to an instance?
 
You'll want a slightly different design where you keep a SandHopper object and instead of creating a new one each time you set it visible again. You can wrap this into a static method similar to this:

:

class SandHopper {
  private static SandHopper onlySandHopper;
  public static SandHopper GetSandHopper() {
    if(onlySandHopper == null) {
      onlySandHopper = new SandHopper();
    }
    return onlySandHopper;
  }

  // .. the rest of your class...
}

With this, you might want to make your constructors private, so that only GetSandHopper() can create one. The benefit to this: you reuse the same one all the time. The drawback: you can only have 1 SandHopper (if you want more than one, you can leave the constructors public, but anybody who calls GetSandHopper() will always use the same one).


All times are GMT -5. The time now is 4:21 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC