Question

I have a class extending AbstractTableModel and it pools Data from a Database, Since its a Swing Component I initialize it in EventQueue, the Problem is most of the operation such as getting Connection and Querying DB all runs in eventqueue hence it takes time to load the JTable. Is there a way to separate the two processes.

Was it helpful?

Solution 2

If you need to do a time-consuming operation, to prevent the GUI from freezing you have to do it in a thread different than the Event Dispatcher Thread. Those threads are called Worker threads, an example on how to use them is detailed in this other question.

Edit: I have found a very nice introduction and example article here.

OTHER TIPS

Use a SwingWorker for doing the heavy background tasks to avoid blocking the EDT.

You can set up the table in a thread separate from the event queue like this:

new Thread() {
    public void run() {
        // setup the table
    }
}.start();

This will result in the code in run being run in a new thread, which is what you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top