Question

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...

  • i use database to retrieve all values to jtable -

edited : now I used SwingWorker, but i have another problem :

  1. When the UI is showing, the master table display a row (this is true)
  2. When i try to clicked that row, it dont clicked, i mean usually u will get a background in a selected row, but not with me (wrong->my problem)
  3. It also throw NullPointerException at my child table
  4. But when i wait for a while, it suddenly work again...

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 :)

Was it helpful?

Solution

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();       
}

OTHER TIPS

Usually, I use a MouseListener (mouseClick event) to work with JTables. First, when you click in the first table, you save the object that represent a student in that line and tells the second table do update. The same thing will be done to the second. For each click, you save the semester object and updates the third table. Off course, you can do the same thing using the ListSelectionListener. I think you are already doing this, but you need two different event listeners. One for the first and another for the second table.

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