javaswingjoinjtabletablemodel

How to Join data from 2 different tables in a single JTable


Im making a program for employee management system.. Now I am making the CRUD form for Employee registration, i seperated the src code in data layer , business logic and gui.. In gui I separated gui view and gui model, in Gui model i created a EmployeeTableModel , to specify how I want to show data for employee Table , but in that table i got datas that come from more than one table , one is Employee table from database another one is Phone .. I have successfully get the data from Employee Table , and i can put those data in both Employee Table and Phone table in database , but I can't get them into the TableModel, I can only get the datas from employee.

PHOTO OF THE TABLE

ALL SOURCE CODE HERE - GIT

I tried changing the code in my EmployeeTableModel to accept more columns and to make a way to add datas from telephone table too , but didn't worked because each employee has 2 -3 phone numbers and the Employee_ID is specified as a foreign key on Phone table ..Each employee has 3 phones as I said and when we specify which phone we want to get , it's depenedent from employee_id that is foreign key in Phone table.

public class PunetoriTableModel extends AbstractTableModel {

List<Punetori> list;
TelefoniRepository tr = new TelefoniRepository();
PunetoriRepository pr = new PunetoriRepository();

String[] cols = {"Nr.", "Nr-Departmentit", "Emri", "Mbiemri", "Email", "Qyteti","Adresa","tel1","tel2"};

public PunetoriTableModel() {
}

public PunetoriTableModel(List<Punetori> list) {
    this.list = list;
}

public void addList(List<Punetori> list) {
    this.list = list;
}

@Override
public String getColumnName(int col) {
    return cols[col];
}

@Override
public int getRowCount() {
    return list.size();
}

public void remove(int row) {
    list.remove(row);
}

public Punetori getPersoni(int index) {
    return list.get(index);
}

@Override
public int getColumnCount() {
    return cols.length;
}

public String getDateToString(Date d) {
    DateFormat da = new SimpleDateFormat("dd-MM-yyyy");
    return da.format(d);

}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
     Punetori p = list.get(rowIndex) ;


        switch (columnIndex) {
            case 0:
                return p.getPunetoriID();
            case 1:
                return p.getDepartmentiId();
            case 2:
                return p.getEmri();
            case 3:
                return p.getMbiemri();
            case 4:
                return p.getEmail();
            case 5:
                return p.getQyteti();
            case 6:
                return p.getAdresa();




            default:
                return null;
        }
    }
}

Solution

  • First of all decide what your columns are going to be. You seem to be confused what these columns should be, when some employees have more than one phone. It is not clear what your problem is, but probably just drawing it on a piece of paper will already clear your mind.

    Decide also whether these columns are going to be fixed, or dynamic (i.e. is it possible to get more columns for some employees, that you will need to add to your table?)

    Second, create a separate data structure, lets call this class EmployeeData, that captures the information you want. Make this separate from the PunetoriTableModel. Populate this from your data repositories. Inside this put the necessary logic that decides which phone to use, or whatever custom business logic you need.

    Make the EmployeeData provide a few public methods like getColumns(), getRowCount() and getRow(int i) etc.

    In the constructor of your PunetoriTableModel just pass the EmployeeData and inside the respective methods call the EmployeeData ones. This way the table model sees the data as a table, and you have your custom logic which combines it from different tables into one which is separate.