Again, as noted in your prior deleted question,
- Your code is not thread safe as you are making Swing calls that change the state of Swing components off of the Swing event thread.
Your question is an XY Problem in that you are asking about specific code solutions that may be wrong, and you have not discussed your over all goal.
- You will want to read this article about Swing thread safety: Concurrency in Swing.
- You should consider going into greater detail into just what behaviors you'd like your users to experience with your GUI, so we can perhaps be able to provide a better more robust solution.
- For better help, consider creating and posting a minimal example program, code for us to review, test, and possibly fix.
For instance, you could often start and stop Threads with a SwingWorker background thread. For details on how to use this tool, please read the SwingWorker tutorial which I've linked to above, and which you can find here.
An example of a minimal Swing program with a SwingWorker that sort of does what you're trying to do (I think!):
import java.awt.event.*;
import javax.swing.*;
@SuppressWarnings("serial")
public class StopThreadGui extends JPanel {
private JTextField field1 = new JTextField("Hello", 10);
private JTextField field2 = new JTextField("Goodbye", 10);
private JButton startButton = new JButton(new StartAction("Start", KeyEvent.VK_S));
private JButton endButton = new JButton(new EndAction("End", KeyEvent.VK_E));
private MySwingWorker myWorker;
public StopThreadGui() {
add(field1);
add(field2);
add(startButton);
add(endButton);
}
private static void createAndShowGui() {
StopThreadGui mainPanel = new StopThreadGui();
JFrame frame = new JFrame("StopThreadGui");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
private class StartAction extends AbstractAction {
public StartAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent evt) {
if (myWorker != null && !myWorker.isDone()) {
return;
}
String text1 = field1.getText();
String text2 = field2.getText();
myWorker = new MySwingWorker(text1, text2);
myWorker.execute();
}
}
private class EndAction extends AbstractAction {
public EndAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent evt) {
if (myWorker == null || myWorker.isDone()) {
return;
}
myWorker.cancel(true);
}
}
}
class MySwingWorker extends SwingWorker<Void, Void> {
private static final long SLEEP_TIME = 400;
private String text1;
private String text2;
public MySwingWorker(String text1, String text2) {
this.text1 = text1;
this.text2 = text2;
}
@Override
protected Void doInBackground() throws Exception {
boolean foo = true;
while (foo) {
System.out.printf("Text1: %s; Text2: %s%n", text1, text2);
System.out.println("Close me please..");
Thread.sleep(SLEEP_TIME);
}
return null;
}
}