tags:

views:

51

answers:

2

Hello everybody, I face a problem in using AbstractTableModel, i use linked list as a container for data , and i fetch records from table from db then i put it in linked list in table model then i male jable.setModel(model). THE PROBLEM i face that the last record i fetch from table from db is repeated n times since n is the number of records in database. and when i make model.getIndextOF(anAppointmentDate); each time return 0 , so i think it each time the object in inserted in the same place ? i may be wrong , but that what i think public class appointmentModel extends AbstractTableModel {

List<appointmentDate> patientAppointment;

public appointmentModel() {
    patientAppointment = new LinkedList<appointmentDate>();   // linked list
}

public int getRowCount() {
    return patientAppointment.size();
}

public int getColumnCount() {
    return 2;
}

public String getValueAt(int rowIndex, int columnIndex) {
    if (columnIndex == 0) {
        return patientAppointment.get(rowIndex).getDateOFAppointment();
    } else if (columnIndex == 1) {
        return patientAppointment.get(rowIndex).getTimeOfAppointment();
    } else {
        return "Not found";
    }
}

public String getColumnName(int column) {
    if (column == 0) {
        return "date";
    } else if (column == 1) {
        return "time";
    } else {
        return "not found";
    }
}

public void insertRecord(appointmentDate anAppointmentDate)
{
    list.add(anAppointmentDate);  
    fireTableRowsInserted(list.size()-1, list.size()-1);
    System.out.println(list.indexOf(anAppointmentDate)); // each time it prints 0        
}

////////// here where i use the model

while (resultSet.next()) {
           N_Date= resultSet.getDate("appointDate");
           anAppointment.setDateOFAppointment(N_Date);

            N_time = resultSet.getString("appointTime"); 
            anAppointment.setTimeOfAppointment(N_time);
            tableModel.insertRecord(anAppointment);

           }
             jTable.setModel(tableModel);      

The output i have is the last record repeated in all table records ,plz help!

+1  A: 

Found the problem!

You don't show quite enough code around "where you use the model" but it looks like you are re-using the same object for each insert. You've got this object, you set its attributes from what comes out of the DB, and then you stash it in the list.

Well guess what: It's the same object every time, so it's bound to have the same value every time! If you want different values in your list, you'll need to use distinct objects. Don't worry, this is a common enough beginner's mistake.

A sensible way to do this would be to construct a new appointmentDate for every DB record. You may want to initialize the data right in the constructor.

Finally, note that if appointmentDate is a class name, it should (by convention) begin with an uppercase letter!


Finally (some more): LinkedList is efficient at inserts, but performs horribly at retrieval, i.e. the get(n) thing. I'd expect there to be a lot more scrolling and displaying from your list than inserting, so I'd use the List implementation that's much better performing here: ArrayList. It works the same, is only marginally slower at inserts and much faster at retrieval. But that's just additional information for you. Performance isn't so important for a small project like this.

Carl Smotricz
Finally it works correctly , thaanx
Alaa
+1  A: 

You are using the same reference to an AppointmentDate object, and change only its internal state.

Thus everytime you add the same object to the list. So with the last loop iteration you set all objects inside the list to have the data of the last row.

To fix this, instantiate a new AppointmentDate() on each loop iteration:

while (resultSet.next()) {
    AppointmentDate anAppointment = new AppontmentDate();
    // the rest is the same
}

Note also that the class name should start with an upper-case letter - i.e. AppointmentDate, not appointmentDate.

Bozho
Finally it works correctly , thaanx
Alaa