Forums Register Login

Restarting Java Application

+Pie Number of slices to send: Send
I have  a simple request. I just want to know how to restart an application from start using jave code.

Thanks for helping in advance.

+Pie Number of slices to send: Send
I have a simple button, and all I want to do with it is to restart the application.
+Pie Number of slices to send: Send
 

Anthony Johnson wrote:I have  a simple request. . . .

I don't think you do. I think it requires a lot of explanation of what you mean by restart before we can help.

Don't use System.exit. You are supposed to be in a multi‑threaded environment when you are doing any Swing coding, and you can do serious harm with System.exit. Apart from the fact that System.exit makes it impossible to restart anything.
+Pie Number of slices to send: Send
Ok well in that case, how do I make it rerun the program in another window.
+Pie Number of slices to send: Send
Don't know. If you put anything in a loop, you are liable to have an endless loop running in the EDT and your performance will be very slow. You can't try anything asynchronous because Swing isn&apost thread‑safe. The nearest I can think of is to add a window listener: in the window closed method put a call to set up the GUI again. You would then end up with a recursive call of the set up GUI method. Or something like that.

It strikes me as strange that you would want to close a GUI window and then start the GUI again. Why?
+Pie Number of slices to send: Send
 

Anthony Johnson wrote:Ok well in that case, how do I make it rerun the program in another window.


Do you mean you want to close the application and restart as a new instance?
+Pie Number of slices to send: Send
You keep saying "the application" and "the program" as if we should know what that means. It's easy to jump to the conclusion that "the application" means that code which you've posted there, but I suspect that's a wrong guess. So I would suggest you step back and provide a more complete description of what you're talking about.
+Pie Number of slices to send: Send
First, you should be creating and displaying the frame from the Event thread, not the main thread.

Second, just eliminate the System.exit() call. Then every time you click on the button, the actionPerfomed() method will be executed.

Third, you will need some way to eventually exit the program, such as adding another "Exit" button and adding an event handler to that button, and that nahdler's actionPerformed() method should call  a method that will exit the application.
+Pie Number of slices to send: Send
thanks Fred Kleinschmidt
Politics is a circus designed to distract you from what is really going on. So is this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 4921 times.
Similar Threads
How to get image (icon) from a JButton
generate table data on button click
Why am I getting an exception?
String is not null, then it is?
Alignment is working only sometimes in editor made in java.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:40:20.