Have the loop test a boolean variable and exit the loop when its value changes. Your loop should be careful to not use the GUI's thread.
Use a key listener or keybinding to set the boolean variable...