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.

有帮助吗?

解决方案 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.

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top