I want to extract data from a table in the database based on the columns I specify using an AbstractTableModel. There are 8 columns in the table but want to only show 3 of the 8 columns.
For example:
The database has these columns: ID, First_Name, Last_Name, ...., Phone_Number. If I specify that I want to display First_Name, Last_Name, Phone_Number in the table it shows me ID, First_Name, and Last_Name. I think I should specify the index of the column name from the database to display the correct column but do not know how to implement that in my custom AbstractTableModel class.
public class PhysicianModelController extends AbstractTableModel
{
PhysicianBroker pb = PhysicianBroker.getPhysicianBroker();
private String[] columnNames = {"First Name", "Last Name", "Phone Number"};
private ArrayList<Physician> ph = pb.getAllPhysicians();
@Override
public int getRowCount() {
return ph.size();
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public String getColumnName(int col)
{
System.out.println(col);
return columnNames[col];
}
@Override
public String getValueAt(int rowIndex, int columnIndex) {
System.out.println(rowIndex + " : " + columnIndex);
Physician p = ph.get(rowIndex);
switch (columnIndex)
{
case 0:
return Integer.toString(p.getEmployeeId());
case 1:
return p.getFirstName();
case 2:
return p.getLastName();
case 3:
return p.getBirthDate();
case 4:
return p.getStartDate();
case 5:
return p.getEndDate();
case 6:
return p.getAddress();
case 7:
return p.getPhoneNumber();
default:
return "Incorrect input";
}
}
public void addRow(Physician p)
{
ph.add(p);
int row = ph.size();
fireTableRowsInserted(row,row);
fireTableDataChanged();
}
@Override
public Class getColumnClass(int c)
{
return getValueAt(0,c).getClass();
}
}
I have overridden the getRowCount(), getColumnCount(), getColumnName(), getValueAt(), and getColumnClass() methods with my implementation. Everything works correctly once I supply all the columns from the database table.
Would someone be kind enough to lend me a hand on how to do this?
Thanks