0

I have created a Java application that goes through hundreds of documents after user clicks "Run" button. Is there a way to terminate the program and leave the GUI running? All I want to be able to stop is the process of reading the documents.

System.exit(0) is not the solution I am looking for as my whole app closes.

  • 1
    Define "terminate the program". – Juan Carlos Mendoza Feb 06 '18 at 14:50
  • 6
    Run the document processing in a new thread and abort it if needed. – Oscar Feb 06 '18 at 14:50
  • My program reads hundreds of blobs from database. I want to be able to stop the program in the middle of it if I wish to. I still want to be able to play around with the GUI and change some bits but I want to stop the process of reading documents I've made a method for. – dsafas fsafasfsa Feb 06 '18 at 14:52
  • 1
    Isn't a GUI a part of your *program*? – PM 77-1 Feb 06 '18 at 14:52
  • @Oscar I'm running it with doInBackground() using SwingWorker. Any solution? – dsafas fsafasfsa Feb 06 '18 at 14:54
  • Well, first of all you should move all your processing logic into separate thread, afterwards take a look at this [link](https://stackoverflow.com/questions/671049/how-do-you-kill-a-thread-in-java). – Aleksandar Feb 06 '18 at 14:54
  • 3
    Have a look at `SwingWorker.cancel(...)` and `SwingWorker.isCancelled()` as well as the JavaDoc on `SwingWorker` itself. – Thomas Feb 06 '18 at 14:56
  • Make a cancel button, make it clickable only when reading, react on the click in an appropriate manner - where is the problem? – user unknown Feb 06 '18 at 15:16

1 Answers1

0

It's difficult to say something without to see your application. But probably this piece of code will help you to understand how to implement what you want:

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.WindowConstants;

public class SwingWorkerTest implements Runnable {

    private JButton cancelButton = new JButton("Cancel");

    private JButton runButton = new JButton("Run");

    private JLabel label = new JLabel("Press 'Run' to start");

    private LongWorker longWorker;

    @Override
    public void run() {
        JFrame frm = new JFrame("Long task test");
        frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        cancelButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                longWorker.terminate();
            }
        });
        runButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                longWorker = new LongWorker();
                runButton.setEnabled(false);
                cancelButton.setEnabled(true);
                label.setText("Task in progress. Press 'Cancel' to terminate.");
                longWorker.execute();
            }
        });
        JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
        bottomPanel.add(runButton);
        bottomPanel.add(cancelButton);
        frm.add(label);
        frm.add(bottomPanel, BorderLayout.SOUTH);
        frm.setSize(400, 200);
        frm.setLocationRelativeTo(null);
        frm.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new SwingWorkerTest());
    }

    private class LongWorker extends SwingWorker<Void, Void> {
        private volatile boolean terminated;

        @Override
        protected Void doInBackground() throws Exception {
            // check special variable tov determine whether this task still active
            for (int i = 0; i < 1000 && !terminated; i++) {
                readFile();
            }
            return null;
        }

        @Override
        protected void done() {
            if (terminated) {
                label.setText("Process terminated. Press 'Run' to restart.");
            } else {
                label.setText("Process done. Press 'Run' to restart.");
            }
            cancelButton.setEnabled(false);
            runButton.setEnabled(true);
        }

        // dummy method - make 10 milliseconds sleep
        private void readFile() {
            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                // Nothing here
            }
        }

        public void terminate() {
            terminated = true;
        }
    }
}
Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48