markiewicz36
markiewicz36

Reputation: 57

Empty JTable of objects

I'm having a problem with the JTable. I'm creating a "library" program and there's a "rental" class. It contains user, book and dates. The problem is that the loans are not displayed.

Rental class:

    public Wypozyczenia(Uzytkownik uzytkownik, Ksiazka ksiazka, Calendar dataWypozyczenia,Calendar dataZwrotu){
    this.uzytkownik = uzytkownik;
    this.ksiazka = ksiazka;
    this.dataWypozyczenia = dataWypozyczenia;
    this.dataZwrotu = dataZwrotu;
    this.uzytkownik.dodajWypozyczenie(this);
}

Class with JTable:

WypozyczenieOddaj tablicaModelKsaizki = new WypozyczenieOddaj(Dane.wypozyczenia);
tabelaKsiazki.setModel(tablicaModelKsaizki);

--

class WypozyczenieOddaj extends AbstractTableModel {

private List<Wypozyczenia> li = new ArrayList();
private String[] columnNames = {"Imie", "Nazwisko"};

public WypozyczenieOddaj(List<Wypozyczenia> list) {
    this.li = list;
}

@Override
public String getColumnName(int columnIndex) {
    return columnNames[columnIndex];
}

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

@Override
public int getColumnCount() {
    return 2;
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
    Wypozyczenia x = Dane.wypozyczenia.get(rowIndex);
    switch (columnIndex) {
        case 0:
            return x.getUzytkownik();
        case 1:
            return x.getKsiazka();
    }
    return null;
}

result

I'm sure the rental adds up because the toString() works.

Upvotes: 0

Views: 32

Answers (1)

afizerian
afizerian

Reputation: 11

I haven't seen you specifying the source of the data to be filled to the JTable i.e where the system will get the values of the rentals from. Either a list or from your database.. You can simply do that by using

Object [] data = {user, book}
tablicaModelKsaizki.addRow(data)

Upvotes: 1

Related Questions