You should run GUI related code in Event Dispatch Thread (EDT). Do not create another thread.
public void actionPerformed(ActionEvent evt) {
try {
SwingUtilities.invokeLater(
new Runnable() {
...
If actionPerformed
is the listener callback, it already runs on EDT. Create another
Runnable
if the callback takes long to execute, or it will "slow down" the GUI.
If MyFunction()
method is not related to GUI repaint or anything like that,
run it on another thread, specially if it is resource intensive.