Replacing a running program with an fresh instance of itself
I am attempting to setup a reset method within a program that essentially creates a replacement of itself and then closes itself down leaving the replacement running. I know that normally to do this I could use some outside driver class and have no issue, but I am seeing if it is possible to create a completely independent instance of a class with itself.
If I create a new instance and then exit the existing instance, the newly created instance exits as well. I would imagine that this is possible but I can not find any way to go about it at this point.
The particular program I am working on is a Swing GUI and I have set the default close operation to EXIT_ON_CLOSE.
The reason that I wish to do this is that it would be more simple to just replace the current window with a new one than it would be to go all the way through and reset everything back to de开发者_运维技巧fault.
Also on a purely theoretical note I would like to find out i this is possible.
Quick Solution: don't use EXIT_ON_CLOSE as your default window close option. Check the API and choose a better one such as JFrame.DISPOSE_ON_CLOSE.
You state:
The reason that I wish to do this is that it would be more simple to just replace the current window with a new one than it would be to go all the way through and reset everything back to default.
Which leads to a longer potential discussion: you probably don't want to do this, to exit one window and pop up another. Besides being annoying, there are probably much better ways to achieve this end. For one, your GUI should be based on a model, and resetting the model should be fairly easy if it is well written. If not, then consider refactoring it so it is easy. We can help you with this.
精彩评论