Forum Stats

  • 3,824,914 Users
  • 2,260,439 Discussions


Swing and batch job

Robert Angel
Robert Angel Member Posts: 4,535 Bronze Crown
edited Nov 3, 2017 8:13AM in New To Java


I have written a java class that takes some considerable time to do what it does.

I want my end user to invoke this, amongst other functionality, from a java swing form.

I have developed my form.

I have used SwingWorker to launch a thread to run my batch job, which works okay.

Without tieing up the form I want the job to run so the user can perform other actions in the form, whilst the batch job is running. I also want the form to check in the background periodically if the batch job has finished successfully, or failed with error. Note my job may run for over an hour, so tieing up the form is not an option....

My problems are; -

The form freezes on launch of the background process

I am not sure if this approach will work with a batch process that does some fairly heavy lifting...

I am not sure if this is the best way to have a 'concurrent request' equivalent

I am using a thread for my batch process but from reading around threads suspect the job may be too 'heavy' for a thread, is there something I would be better using in this case?

Constructive suggestions gratefully received!

Code excerpt as below; -

public void actionPerformed(ActionEvent e){ 
                b.setText("Batch Launched");
                Progress prog =new Progress();
         prog.status ="Launched Daily Backup Routine";

     //swing safe thread

      SwingWorker<Boolean, Void> worker = new SwingWorker<Boolean, Void>() {
      protected Boolean doInBackground() throws Exception {
    // run batch job here

                  return true;

   // Can safely update the GUI from this method.
         protected void done() {
             boolean status;
           try {b.setText(prog.status);
     // Retrieve the return value of doInBackground.

    //think the below is my biggest problem that freezes the form
             status = get();
        if (status = true){b.setText("Completed backup successfully");
        else {b.setText("Backup failed");}

            } catch (InterruptedException e) {
     // This is thrown if the thread's interrupted.
          catch (Throwable e) {
     // This is thrown if we throw an exception
     // from doInBackground.


  • morgalr
    morgalr Member Posts: 457
    edited Oct 31, 2017 6:48PM

    There are a few things I have to ask, and forgive me they are very basic, but they need to be asked when I start trouble shooting threads and interaction with the main program.

    1 - is your form modal?

    2 - do you know how to operate your debugger?

    2a - have you run your code using your debugger to see what is happening?

    2b - what is the result of running your code in the debugger?

    3 - what kind of resource load is presented when this is run?

    4 - have you had success in creating and running multi thread applications before?

  • Robert Angel
    Robert Angel Member Posts: 4,535 Bronze Crown
    edited Nov 1, 2017 5:57AM

    Hi Morgair,

    To confirm, I am on the beginner slopes of java.

    1. No, not modal - from your comment I have done some reading around modal - this is my problem yes, I need to use jdialog rather than jframe and ensure it is modal?

    2. On a site with no java IDE available and tech support which is glacially slow, having to do it all with notepad...

    2a / 2b - cf above

    3. Light, it is calling actions via an API which then happen in the cloud

    4. Multi thread applications yes, fine no problem, but this is my first attempt with swing

    thanks for your input,


  • morgalr
    morgalr Member Posts: 457
    edited Nov 1, 2017 4:36PM


    No, I do not believe you need to use a JFrame as modal.  If I understand correctly, you want to click buttons on your GUI and have it launch processes, and periodically you want it to check to see if those processes are complete; I do not see where a modal dialog would be best for that.

  • morgalr
    morgalr Member Posts: 457
    edited Nov 1, 2017 4:40PM

    If you can, I suggest you download an IDE with an integrated debugger, like NetBeans, and install it yourself if you have rights on the local box to do so.  Using a debugger can really make a world of difference.

    The JDK used to come with a stand alone debugger, it may still do so, check your version and see if it does. If you can get it working for you, that might be your quickest route to seeing the problem, and ultimately finding the solution.

    Robert Angel
  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited Nov 2, 2017 12:02AM


    The community version of IntelliJ from jetbrains in zip version can be used even if you do not have admin rights to install things. For swing based development, this should be sufficient.


    Robert Angel
  • Robert Angel
    Robert Angel Member Posts: 4,535 Bronze Crown
    edited Nov 2, 2017 7:48AM

    Yes, essentially the form serves as a switchboard for the user to launch long running jobs, which run in threads, and then I want the form (without freezing) to monitor the threads and report their ongoing progress back to the user.

  • Robert Angel
    Robert Angel Member Posts: 4,535 Bronze Crown
    edited Nov 2, 2017 7:51AM

    Thanks, appreciated, I will give this a go!

  • mNem
    mNem Member Posts: 1,380 Gold Trophy
    edited Nov 2, 2017 2:54PM


    off topic:

    There is a Java MOOC just started, just thought of passing the message in case if you are not aware of it ...

    Robert Angel
This discussion has been closed.