-1

I have a program that I am terminating with the System.exit(0); command.
When this happens the JPanel closes. I would like it to rename open so I can view the state at termination. Is there a way of keeping the Jpanel open or is there a better command than System.exit()?

not sure why a down vote I asked a simple question and someone answered it. I can't do it that way so try something else. Going to use a true false to test where to enter the simulation loop.

user3137110
  • 339
  • 1
  • 2
  • 12
  • The JPanel is part of the JVM. System.exit terminates the JVM. If you don't want the JPanel to disappear you can't terminate the JVM. – Compass May 16 '16 at 18:40
  • To know what to include with your question so that you can get better answers, please have a look at [how to ask good questions](http://stackoverflow.com/help/how-to-ask) sections. – Hovercraft Full Of Eels May 16 '16 at 18:45
  • Re `"not sure why a down vote I asked a simple question and someone answered it."` -- I'm not sure either, but it may have something to do with your question remaining very unclear, forcing folks to post unclear answers. Again, please try to improve it so that we understand the actual problem **and the code**. – Hovercraft Full Of Eels May 16 '16 at 19:05
  • And your question is not simple at all, not as it's currently worded. – Hovercraft Full Of Eels May 16 '16 at 19:06
  • Why not log the contents of JPanel before you exit? You could log it with something like: "Program exiting. Contents of panel was: "ContentsOfPanelHere"... or, if the contents aren't textual, you could write out an image of the JPanel contents to be reviewed later – Brian Pipa May 16 '16 at 19:15

3 Answers3

4

regarding:

Is there a way of keeping the Jpanel open or is there a better command than System.exit()?

The best solution: Don't call System.exit(...). Why? Because System.exit(0) closes the JVM, and so all Java processes running on that JVM will shut down when System.exit(0) is called.

As for "better command", that all depends on your need. If you just want to close a window such as a JDialog, then call myWindow.setVisible(false);. If you want to close it and release resources, then myWindow.dispose();.

Note: I suspect that you might have multiple windows open, perhaps multiple JFrames. If so, I strongly urge you to read: The Use of Multiple JFrames, Good/Bad Practice?

You also posted in comments:

I would like to keep the Jpanel open, but stop the simulation from running. I need to stop the Sim when certain conditions are met. so I wrote a stop()

So your question is in fact an XY Problem where you ask how to solve a specific code problem (keep a JPanel open after calling System.exit(0)) when the best solution is to use a completely different approach. Better that you tell us the overall problem that you're trying to solve rather than how you're currently trying to solve it, because System.exit isn't going to be part of the best solution.

Likely the best solution is to well separate your simulation model from its view (the GUI), to be able to give the model functionality that allows it to stop without closing down the JVM -- impossible for me to say how given our current level of knowledge about your problem -- and then reflect the stopping of the model in the view, again without shutting down the system.

The key to all of this will lie in the details of your current program, including the logic that underpins your simulation, and if you need more specific and likely more helpful answers, you're again going to want to improve your question, providing us with much more specific information about your code, your problem and with posting of pertinent code, preferably as a minimal example program.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I would like to keep the Jpanel open, but stop the simulation from running. I need to stop the Sim when certain conditions are met. so I wrote a stop(). – user3137110 May 16 '16 at 18:40
  • @user3137110: then the solution is to stop the simulation and not the GUI. But how can we suggest how to do this based on what you've posted so far? Your question looks to be *very* incomplete. – Hovercraft Full Of Eels May 16 '16 at 18:41
  • @user3137110: consider telling us the important details as well as showing us the pertinent code (preferably a [mcve]) so we can more fully understand your current problem. Best to do this by editing and improving your original question. – Hovercraft Full Of Eels May 16 '16 at 18:43
2

Have you tried an approach similar to: Do something when the close button is clicked on a JFrame


        import javax.swing.JOptionPane;
        /*Some piece of code*/
        frame.addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
            public void windowClosing(java.awt.event.WindowEvent windowEvent) {
                    //delete this code if you want and replace with .dispose() or anything
                if (JOptionPane.showConfirmDialog(frame, 
                    "Are you sure to close this window?", "Really Closing?", 
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION){
                    //choose to close JVM here if you want
                    System.exit(0);
                }
            }
        });
Community
  • 1
  • 1
Petro
  • 3,484
  • 3
  • 32
  • 59
  • I am creating a true false to run the simulation. I do not understand the down vote. I asked if something could be done, it was recommended not to do it that way and try something different. – user3137110 May 16 '16 at 18:59
  • I did not downvote you friend, JFrame is window and JPanel is a container. The moment the JPanel instance loses its reference, it will be garbage collected. – Petro May 16 '16 at 19:01
  • but I up-voted you for passing along some good ideas in a confusing question. – Hovercraft Full Of Eels May 17 '16 at 00:00
0

Here's a way, by overriding the SecurityManager for the JVM:

//set your security manager
static{
     SecurityManager s = new DontCloseOnExitSecurityManager();
     System.setSecurityManager(s);
}

static class DontCloseOnExitSecurityManager extends SecurityManager{
     public void checkExit(int code){
         //here you can put a check to see if you really do want to close - like if the JFrame is still open.
        if(/*do some check*/ 13 == code)
         super.checkExit(code);
         throw new SecurityException("13 is unlucky, you shouldn't system exit on it.");
       }
     }
 }

You'll need to find an appropriate place to put it in, and also how to do your checks (in checkExit).

Apologies for inaccuracies, I'm not in front of an IDE to test this right now.

Kylar
  • 8,876
  • 8
  • 41
  • 75