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.
Asked
Active
Viewed 121 times
0
-
nitpicking: there is no _JTableModel_ ;-) – kleopatra Jul 30 '13 at 09:23
-
ok.. ;),, "AbstractTableModel" – Alfa Jul 30 '13 at 09:32
-
1you can edit your question :-) – kleopatra Jul 30 '13 at 09:58
3 Answers
1
Use a SwingWorker for doing the heavy background tasks to avoid blocking the EDT.

kiheru
- 6,588
- 25
- 31
-
The API outlines an [example](http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingWorker.html#publish%28V...%29) for `TableModel`. – trashgod Jul 30 '13 at 09:08
0
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.
-2
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.

tbodt
- 16,609
- 6
- 58
- 83
-
2doesn't solve Concurency in Swing, could be Runnable#Thread and with (must be) wrapped into invokeLater (output to methods for Swing GUI) – mKorbel Jul 30 '13 at 07:38
-
Alright, but setting up the connection will take a while, and that shouldn't happen in the EDT. Perhaps there could be calls to `invokeLater` in the thread? – tbodt Jul 30 '13 at 07:40
-
whatever came from JDBC to XxxTableModel (in this case) must be wrapped into invokeLater, [for example (this logic is similair to JDBC, Socket or FileIO, covering all rulles for EDT and Concurency in Swing)](http://stackoverflow.com/a/6060678/714968), otherwise you have to bothering with SwingWorkers black hole, but output (publish, process, setprocess, done)is on EDT – mKorbel Jul 30 '13 at 07:56