I'm trying to show table from JSONArray. but the result never show up. The table already show up with right column name by calling this initTable() method. The initTable() is as follow:
private void initTable() {
contentPane.removeAll();
PrintTableModel tblModel = new PrintTableModel();
DefaultTableColumnModel columns = new DefaultTableColumnModel();
TableColumn c;
c = new TableColumn();
c.setHeaderValue(columnNames[0]);
columns.addColumn(c);
c = new TableColumn();
c.setHeaderValue(columnNames[1]);
columns.addColumn(c);
c = new TableColumn();
c.setHeaderValue(columnNames[2]);
columns.addColumn(c);
c = new TableColumn();
c.setHeaderValue(columnNames[3]);
columns.addColumn(c);
listTable = new JTable(tblModel, columns);
listTable.getTableHeader().setReorderingAllowed(false);
JScrollPane scrollPane = new JScrollPane();
scrollPane.add(listTable);
scrollPane.setViewportView(listTable);
contentPane.add(scrollPane, BorderLayout.CENTER);
}
And I'm calling the refresh table from a thread like this:
public class RetrievePrintRequest extends Thread {
private boolean speedUp = false;
public void run() {
if (loginId == 0)
return;
try {
String token = generateLink("prc_admin_id=" + loginId);
do {
JSONArray jsonresult = readJsonFromUrl(BASE_URL + "ajax/get_latest_print?code=" + token);
// looping through All elements
// list holding row data
List<PrintModel> printList = new ArrayList<PrintModel>();
for (int i = 0; i < jsonresult.length(); i++) {
JSONObject c = jsonresult.getJSONObject(i);
// Storing each json item in variable
int printId = c.getInt("print_id");
String bookingCode = c.getString("booking_code");
long startDate = c.getLong("start_date");
String patientName = c.getString("patient_name");
String doctorName = c.getString("doc_name");
PrintModel printModel = new PrintModel(
printId, bookingCode, patientName, doctorName, new Date(startDate));
// add rest of the json data to NodePOJO class
System.out.println(printId + ";" + bookingCode + ";" + startDate + ";" + patientName + ";" + doctorName);
// the object to list
printList.add(printModel);
}
PrintTableModel printModel = (PrintTableModel) listTable.getModel();
printModel.refresh(printList); //<--- I expect this to refresh the table content
Thread.sleep(!speedUp ? 10000 : 60000);
} while(true);
} catch (JSONException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
And this is the refresh method inside TableModel (Add the whole PrintTableModel class)
protected class PrintTableModel extends DefaultTableModel {
private List<PrintModel> listData;
public void refresh(List<PrintModel> data) {
listData = data; // <== refresh with new List and call fireTableDataChanged
fireTableDataChanged();
}
public Object getValueAt(int row, int column) {
Object result = null;
PrintModel model = (PrintModel)listData.get(row);
switch (column)
{
case 0:
result = model.getBookingCode();
break;
case 1:
result = SDF.format(model.getAppointmentDate());
break;
case 2:
result = model.getPatientName();
break;
case 3:
result = model.getDoctorName();
break;
}
return result;
}
public Class getColumnClass(int column) {
switch (column)
{
case 0:
return String.class;
case 1:
return String.class;
case 2:
return String.class;
case 3:
return String.class;
}
return null;
}
Anyone can spot my error? Many thanks.