0

Possible Duplicate:
How can I restart a Java application?

i am working on a java program which takes in bunch of values from the user and returns an answer. i have a "clear" button which makes the application ready to for a new set of input. So what i want is that this button should close the frame currently being viewed(create a window closing event and so on) and then call the main() function to create a new instance of my application. How should i do it? I tried this but it closes everything and the new frame doesn't show up:

WindowEvent wev = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
main(newInstance);

and also what does Toolkit do? suggest some readings for that. Thank you.

Community
  • 1
  • 1
Sahil Chaudhary
  • 493
  • 2
  • 10
  • 29
  • 2
    Do you really need a new instance of the application? Should you not have your application handle this by clearing any text fields and restarting? I think if you keep calling your main again each time you're going to end up with memory leak issues as you build a stack of mains running. Are you using swing? What is wrong with just setting the text of any text fields back to their default? – ThePerson Nov 27 '12 at 10:48
  • yes but the application is actually big, it has three classes and a LOT of variables and i tried that. It was confusing and exhausting. just clearing the textfields and setting variables to default is not helping. besides i am also drawing in one of my panel, and then i have to call repaint and then yes lots of work. This is neat and easy :P – Sahil Chaudhary Nov 27 '12 at 10:51
  • 1
    Did you use any kind of coding structure such as MVC? I think perhaps it may be useful in future to use a real structure so that you can easily do these things. I'll post an answer which may work for you, but I with a valid structure you would be able to just create new models, views or controllers as you wish, rather than needing to restart your application. – ThePerson Nov 27 '12 at 10:54

1 Answers1

0

as per my comments I don't think this is best practise, but it is possible.

This has already been answered here: How can I restart a Java application?

I hope this helps,

Community
  • 1
  • 1
ThePerson
  • 3,048
  • 8
  • 43
  • 69
  • 1
    This is not an answer, please flag the post as duplicate and link the question instead. – Kai Nov 27 '12 at 10:56
  • Thanks, I didn't see that before, I appreciate that. I've flagged it as a duplicate. Hopefully this helps the poster. – ThePerson Nov 27 '12 at 10:57
  • you guys didnt get me, i am not rying to "restart" i was just trying to do this, so simple:myFrameInstance.setVisible(false); main(newInstance); – Sahil Chaudhary Nov 27 '12 at 17:09