Java button: Reset not working

Ok, so I create this program, when you press the reset button, it closes the program and opens a new same program from startup, however I can't figure out how to do it: / Here is my code for the button. This code mostly exits the first program, but it doesn't open it again in a new application.

 button1.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {


                        System.exit(0);
                        new Tests();
                    }

                });

      

+3


source to share


2 answers


This code mostly exits the first program, but does not open it again in a new application.

System.exit(0);

terminates the current JVM process. All instructions after that will not be executed.



If you want to restart your application, you must run a command that starts your application's JVM. If this bank: java -jar yourJar -cp yourClasspath

. You can achieve this with an instance ProcessBuilder

.

Another way is not to restart the application, but to set the state of your application to its original state.

+4


source


System.exit(0);

kills your entire program. Don't use it until you're done.



In a loop, you want to put your entire program (at least the part you want to execute). When you hit the reset button, you will be taken back to the top (or wherever you want) of the loop. Just remember that you have an exit condition to kill the loop, otherwise it will go on forever.

+2


source







All Articles