I wanna ask problem about list selection listener. In my application, I have 3 Jtable, lets say the first table is Student table, filled by student information, the second is semester table, filled by some semesters to the correspondent student (semester 1 to x), and the last table is result table, filled by result on the correspondent semester.
What i want to do is when I click a row in student table, semester table will update it's data, for example, it will filled by semester 1 to 6. Then, when i click a row in semester table, the result table will update it's data.
I can do it from student table to semester table with listselectionlistener and overriding valueChanged method. But how should i do for the same for semester table to result table? I'm getting stuck on this...
edited : now I used SwingWorker, but i have another problem :
i've uploaded my sample code, and remove unrelated code here http://dl.dropbox.com/u/67181952/mycode.java
2nd code for error in child table http://dl.dropbox.com/u/67181952/spk.java
I hope i explained well, sorry for my bad english.
Thanks for any help :)
You can use ListSelectionListener
added to selection models that you can get with getSelectionModel().
Maintain the logic of presentation using table models. Once selection in master
table changes update model/models of details
tables accordingly. As a result of the data update, the model will notify its view (the table) about the changes. And view will refresh accordingly.
Read more about models in How to Use Tables.
EDIT:
Since you load the data from database be sure to do it not from Event Dispatch Thread (EDT) to avoid performance issues. Look into SwingWorker to perform lengthy operations on a dedicated thread. Read more about Worker Threads and SwingWorker.
EDIT: following code upload
It seems that you misinterpreting SwingWorker
specs. execute()
method is not blocking, it schedules worker execution and returns immediately. UI updates should be done in process()
or done()
methods, which are invoked by the worker on EDT. Below is a revised version of one of the of functions from the code you posted:
public void retrieveDetailTransaction(final String noTrans){
SwingWorker<List<TransactionDetail>, Void> worker =
new SwingWorker<List<TransactionDetail>, Void>() {
public List<TransactionDetail> doInBackground() {
List<TransactionDetail> listTransD = transDetailControl.select(noTrans);
return listTransD;
}
public void done() {
try {
listTransDetail = get();
transDetailModel.setListTransaction(listTransDetail);
transactionDetailTable.setModel(transDetailModel);
} catch (InterruptedException ex) {
ex.printStackTrace();
} catch (ExecutionException ex) {
ex.printStackTrace();
}
}
};
worker.execute();
}