Refreshing a JTable

66 views Asked by At

I'm loading my JTable data like that :

ColisDAO colisDAO = new ColisDAO(DatabaseConnection.getInstance());
Object[][] colisData = new Object[colisDAO.count()][];

public ColisPanel() {
        Set<Colis> listColis = colisDAO.getAllColis();
        int i = 0;
        Iterator<Colis> iterator = listColis.iterator();
        while(iterator.hasNext()) {
            Colis currentColis = iterator.next();        
            colisData[i] = new Object[]{
                currentColis.idColis(), currentColis.idDescriptionColis(),
                currentColis.affectataire(), currentColis.module(), currentColis.optionnel(), currentColis.secteur(),
                currentColis.designationColis(), currentColis.designationContenu(), currentColis.poid(), currentColis.valeur(),
                currentColis.iata(), currentColis.projection(), currentColis.observation()};

            i++;
        }

        initComponents();
        colisTable.setRowSelectionAllowed(true);
    }

        colisTable.setModel(new javax.swing.table.DefaultTableModel(
            colisData,
            new String [] {
                "idColis", "idDescriptionColis", "affectataire", "module", "optionnel",
                "secteur", "designationColis", "designationContenu",
                "poid", "valeur", "iata", "projection", "observation"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false,
                false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });

Is is possible to bind the JTable with Colis object directly instead of using an array of array of Object ?

Also, I looked around about reloading the Jtable data after deleting a row, and I understand why the fireTableDataChanged method doesn't works. Because I'm just not updating the model.

How can I do it ?

1

There are 1 answers

0
Jhonatan S. Souza On

First, try to use javax.swing.table.AbstractTableModel, don't use anymore DefaultTable model.