1

I have a problem in my swing app.

I have a SwingWorker that has a for loop inside, that launches 300+ oracle database requests and populates several JTables with the results.

If I keep the swing app window not-minimised, or at least partly visible in the windows explorer, the batch completes fine. Now, if I minimise the app, and then go back to the swing app, it will be frozen.

Basic outline and colours of the components will be visible, most of the window will be just of the background colour I set to it (black) and no text will be visible. The only way to kill the app is by killing the process, since I clicking on 'X' button will not shut the window down.

Is this a common issue? How do you prevent it?

Loop inside the batch worker:

for(int i=1; i<=maxDepth; i++){
    String[] result = getAllLists(database, i);
    for(int j=0; j<result.length; j++){
        String period=result[j];
        for(String name : names){
            System.out.println("New Query: "+name+ " " + period + " | " + "Loading " + (days) + " days x " + years + " years --- ");
            if(isValid(period,name)){
                List<TickHistory> queryResult = model.getByDaysMultiple(name,period,days+mod+daysHeadroom,years, false);
                getModelTableData(name, period, DatabaseHelpers.dateToString(lastCob), years,days,queryResult);
                populatePricesTable(queryResult, days, false);
                view.setNameText(name);
                view.setPeriodText(period);
            }else{
                System.out.println("query invalid");
            }
        }
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
LucasSeveryn
  • 5,984
  • 8
  • 38
  • 65
  • 1
    Could you show us some code please? Probably you're missing `SwingUtilities.invokeLater()`. Checking the app with jvisualvm could help too. – Tamas Rev Jul 21 '16 at 08:47
  • 1
    Any exceptions? Are you using `publish` & `process` correctly? – Catalina Island Jul 21 '16 at 08:58
  • @TamasRev I use invokelater in the main process where I create the main frame, controller and the object I use to connect to the database – LucasSeveryn Jul 21 '16 at 09:00
  • @CatalinaIsland I will check for exceptions, I can't see them as I redirect all console output to GUI text box. I don't use publish and process nowhere in my code. – LucasSeveryn Jul 21 '16 at 09:00

1 Answers1

3

I don't use publish and process anywhere in my code.

This is a likely source of the problem. SwingWorker relies on calling publish() in doInBackground() and using process() to update the TableModel on the event dispatch thread. A complete example is examined here.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I added the loop from inside of the batch worker to the question. You can see the methods `getByDaysMultiple` which pulls data from db, then there is `getModelTableData` which fires another few requests, and then `populatePricesTable` which populates JTable, and some view methods below as well. It's my first ever swing app so I appreciate your explanations. – LucasSeveryn Jul 21 '16 at 09:21
  • You must not update the `TableModel` from `doInBackground()`. You will have to re-factor your code to `publish()` intermediate results as shown in the [turorial cited](https://docs.oracle.com/javase/tutorial/uiswing/concurrency/interim.html). If you have problems, please edit your question to include a [mcve], based on the [example cited](http://stackoverflow.com/a/34742409/230513), that shows your approach. – trashgod Jul 21 '16 at 09:29