0

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.

  • 1
    The posted code looks reasonable, but we don't know the context of how the code is used. Maybe you have two instances of your JTable, one that is visible on the JFrame and one just sitting in memory. Do a System.out.println(table) a) when you add the table to the frame and b) when you set the model on the table. Make sure it is the same instance. Maybe you have two instances of your class that creates the table. Many times I see people create a class that extand JFrame and then in that class then create a JFrame. The point is without context we can't tell what is happening. You need to debug. – camickr Jun 16 '20 at 22:26
  • I found an error. when creating the object, it didn't add itself to the list. My mistake. Thanks for answer. – markiewicz36 Jun 16 '20 at 22:33

1 Answers1

1

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)
afizerian
  • 11
  • 4