Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 2nd, 2008, 9:12 AM   #1
namsu
Newbie
 
Join Date: Jan 2008
Posts: 15
Rep Power: 0 namsu is on a distinguished road
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.
namsu is offline   Reply With Quote
Old Apr 2nd, 2008, 10:17 AM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
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).
__________________
<insert disclaimer here>
<insert shameless plug for Visual Studio here>
Jimbo is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Return type - main() / ISO C standard sharadpro C 2 Mar 14th, 2007 6:02 AM
FTP and return code fetching Serinth C 2 May 28th, 2006 11:05 PM
Function crashes on return Polyphemus_ C++ 5 Nov 9th, 2005 3:03 PM
using `char' return type in functions atreyu C 6 Jul 22nd, 2005 12:13 PM
return a value from a function paulmedic555 C++ 1 Jan 22nd, 2005 7:00 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:08 AM.

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