Reputation: 2472
Is there any known problem if I use an infinite for loop within a button click event? Actually, first I set up the program by taking settings from the GUI. Then when I click start button, I want a function to be called continuously. That function is not part of the GUI. it's a function from the remaining part of the program.
Upvotes: 0
Views: 1357
Reputation: 692151
Yes there is: the GUI will completely be frozen. You have to execute long-running tasks in a background thread. This is usually done using a SwingWorker. Have a look at the Swing tutorial to learn how to use them.
Upvotes: 1
Reputation: 74800
The problem is that your event handler (ActionListener, here) is called in the AWT event dispatch thread (EDT).
This thread is the same thread that handles all the user interaction, as well as repainting your application. If you don't return soon from the action listener, your application will seem frozen.
Don't do that, use a new thread instead. (For GUI updating, call back into the EDT using EventQueue.invokeLater (or SwingUtilities.invokeLater, this is the same).)
Upvotes: 1
Reputation: 137422
You can do that, but in a separate thread, otherwise you'll block the UI thread and the application will get stuck and force closed.
Upvotes: 1
Reputation: 272752
Yes, your GUI will stop responding.
You should fire off a worker thread to do the computation instead. Recommended reading:
Upvotes: 1