-1

I have one jar implemented for extracting results from DB & write into excel file. My jar has two buttons "Execute" & "Stop". When I am clicking on "Execute" button from GUI my code runs but let us assume that If I want to "Stop" the execution immediately I'll click on "Stop" button and it will perform a System.exit(0). Can someone please help me with the idea of how to start with this?

  • `System.exit()` is rarely a good idea. Normally with Swing you close all your (top level) windows and the system will then exit. – markspace Apr 25 '19 at 18:33
  • Actually, i am looking for an idea like when my Start is doing some job and i want to stop the execution immediately then how to handle this? I will appreciate your answer on this – JackofallTrade Apr 25 '19 at 18:36
  • Why would you want to terminate the app? And in any case it would be: `stopButton.addActionListener(e -> System.exit(0));` for example. It's not hard to do that. Anyway this question is too broad as it's unclear what you're asking – Frakcool Apr 25 '19 at 18:40
  • Hi Frakcool Thanks for your suggestions. Can you please opine on this? Suppose i have started my code by clicking on start button from gui and it will run for 10 mins. But i will hit stop button at 7mins to terminate the execution. How to do that ? I have two buttons start and stop as actionperformed. Appreciate your advise on this – JackofallTrade Apr 25 '19 at 18:45
  • please change the question from "How to Stop a Java Code while It is executing by SWING" to "How to Stop a block of Code while It is executing in SWING" as swing thread EDT thread should never be stopped because it deals with all of the swing demons . – OverLoadedBurden Apr 25 '19 at 19:35
  • Probably related [question](https://stackoverflow.com/questions/55714886/java-swing-gui-is-not-updating-some-properties/55738591#55738591). Again, as I said, how can you terminate the execution? It depends on what you want it to do. Again, unclear what you're asking. How do you hit stop? Just clic the button. How to stop execution? Depends on what the task is. How to do WHAT? And btw, Swing is not an acronym @OverLoadedBurden so, no need to be uppercased. – Frakcool Apr 25 '19 at 21:40
  • i know @Frakcool i just copy --> paste the title and change some in it and he said he it is in a button so review the question please if you think you can provide a code in your answer , am interested in the code part(tried to imagine a case that matches the question but i couldn't it seems like he tries to be the thread manager it self , right ?). – OverLoadedBurden Apr 25 '19 at 21:48
  • There is no answer from me, as OP hasn't provided enough information apart from: "I want to run a process in Swing (I imagine load a HUGE database) and then stop it manually" and OP wants us to provide code when 1) He hasn't do it and 2) He hasn't put enough effort into describing his problem. @OverLoadedBurden – Frakcool Apr 25 '19 at 21:55
  • sorry i got confused between you and FredK , but if in the case you described it's impossible to force the the thread to sleep or awake !@Frakcool – OverLoadedBurden Apr 25 '19 at 22:01

2 Answers2

1

Make sure the task that the "Execute" button starts is run in a separate thread. If you just execute it directly in the ActionListener of the Execute button, you will block the event thread and clicks on the Stop button will not be processed until after the task finishes.

FredK
  • 4,094
  • 1
  • 9
  • 11
0

Use SwingWorker to implement this behavior, it provides ability to execute the tasks in background (a separate thread) and also gives the ability to cancel the task.

Hope this helps!

Sanjeev
  • 9,876
  • 2
  • 22
  • 33
  • Also, be sure to disable the Execute button in its ActionListener, and re-enable it at the end of the task or when the Stop button is clicked. – FredK Apr 25 '19 at 19:21
  • I have the below code for Execute..... JButton btnExecute = new JButton("Execute!!"); btnExecute.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { //my code } }); frame.getContentPane().add(btnExecute); ------------------------ Can you please help me in code level how to put Execute & Stop in separate thread ? – JackofallTrade Apr 26 '19 at 04:45
  • You can google it, lots of examples are around, Couple of them: [Example 1](https://www.geeksforgeeks.org/swingworker-in-java/) and [Example 2](http://www.javacreed.com/swing-worker-example/) – Sanjeev Apr 26 '19 at 14:50