javajtextareainvokeprogressswingutilities

How can I make a progress complete show in a JTextArea while running code that's figuring out what to display in that JTextArea?


I don't know if this is possible. I'm making a lottery application, and I'm trying to use as few GUI components as possible. So I have a JTextArea that is supposed to show the following message (for example):

"Calculating...55.4%"

When I print it to console, it shows just fine, but it won't print it to the JTextArea. I tried to use SwingUtilities.invokeLater but that's not working either.

    for (int x = 0; x < daysBetween; x++)
    {
      completion = "Calculating..." + df.format((100 * (x + 1)) / daysBetween) + "%";
      if (!textArea.getText().equals(completion))
      {
        textArea.setText(completion);
      }
      /*
      Here I have a lot of irrelevant code that compares your tickets to the winning tickets and counts your winnings and other statistics...
      */
    }

    ticketReport += "Computation time: " + getElapsedTime(start, System.nanoTime());
    ticketReport += "\nEnd date: " + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.DAY_OF_MONTH) + "/" + cal.get(Calendar.YEAR); 
    ticketReport += "\nTotal # of tickets purchased: " + numOfTicketsPurchased;
    /*
    etc. with filling out the ticket report
    */
    textArea.setText(ticketReport);

As you can guess, I want the JTextArea to update as I set the text of the textArea in the for loop above. It does not update the JTextArea until the end of the method, which is the very bottom when I set the text area to show the ticket report.

Lottery Fever application

MY END GOAL: I want to eventually turn this into an Android phone application, so that's why I don't want to use any pop-ups or anything.


Solution

  • My original SwingWorker approach made it where it showed the completion percentage status in the JTextArea. I added an option to cancel the operation, and when I attempted to cancel, the "thread" (SwingWorker) would not cancel. When I cancel and restart, it would stack constantly. Each successive starting while the other was going would cause the calculations to go slower and slower, and then give incorrect information.

    The way I did it this second time solved the original problem and this new problem. I created a new private class that extends Thread, overran its run() method, and inserted the code to be executed in the thread in that method. I noticed any method to stop the thread was deprecated because of potential issues, so I created a boolean to be set when the user requests to stop the thread, and inside the thread it periodically reads the value of the boolean, then exits when it equals to true.

    The way I start the thread is by re-initializing the object BackgroundCalc and calling its start() method.

      private class BackgroundCalc extends Thread
      {
        public void run()
        {
          /*
          initializing stuff here
          */
          for (int x = 0; x < daysBetween; x++)
          {
            progress = (100 * (x + 1)) / daysBetween;
            if (destroyCalcThread) return;
            /*
            background calculations here
            */
          }
          /*
          finish up thread here
          */
        }
      }
    

    I'm a little inexperienced with threads, so hopefully my struggle here and the way I explained it will help someone else who's inexperienced with the concept.