0

In a JPanel, I have combobox in order to update the combobox with details. I need to query from backend Database. This makes the entire GUI freeze.

if (ccyPairs.size() == 0) {
  try {
    String query = "SELECT * FROM " + PropertyFile.getInstance().getSmfTable() + " WHERE SECURITYTYPE = 4 ";
    Security[] securities = SecurityMaster.getInstance().getData(query, false);
    for (Security security : securities) {
      String Symbol = security.getSymbol();
      ccyPairs.add(Symbol);
    }
  } catch (Exception exception) {
    Log.error(getClass().getName(), "getValidSymbols", "Exception occurred while retrieving fx symbols from smf table " + exception.getMessage());
  }

  return ccyPairs;
}
return ccyPairs;

Here Security[] securities = SecurityMaster.getInstance().getData(query, false); is a call to database.

Can anyone please explain how to solve this problem?

MMPgm
  • 109
  • 1
  • 1
  • 6
  • Start by having a look at [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) to understand what the problem is and [Worker Threads and SwingWorker](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html) for a possible solution – MadProgrammer Dec 03 '15 at 22:47
  • @MadProgrammer if I use worker.get() does it blocks EDT? – MMPgm Dec 04 '15 at 10:19
  • Yes, it will block until the worker's `doInBackground` method returns or throws an `Exception` – MadProgrammer Dec 04 '15 at 10:41
  • @MadProgrammer But my aim is not to block EDT untill I do DB call – MMPgm Dec 04 '15 at 11:02
  • Then use the works done method to call get (if you want the result) and call some callback to let, who ever is interested, that the worker has completed – MadProgrammer Dec 04 '15 at 19:22

1 Answers1

1

You should use a SwingWorker as explained in Worker Threads and SwingWorker to free up your EDT while the DB fetches data.

Jan
  • 13,738
  • 3
  • 30
  • 55