Data not displayed in table by AbstractTableModel

2

I'm developing a ticket request system, however because I have to add JXDataPicker to the table, I had to implement AbstractTableModel .

The model apparently complies with the documentation, I can display all the fields, but I can not see the data, the line appears but no information.

This is the template configuration.

public class PassagemTableModel extends AbstractTableModel{

    private ArrayList<PassagemAerea> passagem;
    private final String[] colunas = {"Data de Partida", "Data de Retorno", "Aer. de Partida", "Aer. de Retorno", "Horário de Chegada No Local", "Horário de Saída do Local","Tipo de Bagagem"};
    private final Class[] columnClass = new Class[] {
        Date.class, Date.class, String.class, String.class, String.class, String.class, String.class
    };

    public PassagemTableModel(){
        this.passagem = new ArrayList<>();
    }

    public void adicionaPassagem(PassagemAerea passagem){
        this.passagem.add(passagem);
        fireTableDataChanged();
    }

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

    @Override
    public Class getColumnClass(int columnIndex){
        return columnClass[columnIndex];
    }

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


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

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {

        switch(columnIndex)
        {
            case 0: passagem.get(rowIndex).getDataPartida();
            case 1: passagem.get(rowIndex).getDataVolta();
            case 2: passagem.get(rowIndex).getAeroportoSaida();
            case 3: passagem.get(rowIndex).getAeroportoRetorno();
            case 4: passagem.get(rowIndex).getHorarioChegada();
            case 5: passagem.get(rowIndex).getHorarioSaida();
            case 6: passagem.get(rowIndex).getTipoBagagem();
            default: return null;
        }
    }

     @Override
    public void setValueAt(Object Value,int numLin, int numCol) {

        switch(numCol){
            case 0:
               passagem.get(numLin).setDataPartida((Date)Value);
               break;
            case 1:
               passagem.get(numLin).setDataVolta((Date)Value);
               break;
            case 2:
               passagem.get(numLin).setAeroportoSaida(Value.toString());
               break;
            case 3:
               passagem.get(numLin).setAeroportoRetorno(Value.toString());
               break;
            case 4:
               passagem.get(numLin).setHorarioChegada(Value.toString());
               break;
            case 5:
               passagem.get(numLin).setHorarioSaida(Value.toString());
               break;
            case 6:
                passagem.get(numLin).setTipoBagagem(Value.toString());
                break;
        }
        fireTableDataChanged();

    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return true;
    }
}

Here follows the model call in the constructor of the class that contains the table.

    initComponents();

    passagemTableModel = new PassagemTableModel();

    tblSolicitacoes.setModel(passagemTableModel);
    tblSolicitacoes.setRowHeight(40);
    tblSolicitacoes.setBackground(Color.red);
    tblSolicitacoes.setSelectionBackground(Color.PINK);

    TableColumn dataPartidaModel = tblSolicitacoes.getColumnModel().getColumn(0);
    TableColumn dataRetornoModel = tblSolicitacoes.getColumnModel().getColumn(1);
    dataPartidaModel.setCellEditor(new DateCellEditor());
    dataRetornoModel.setCellEditor(new DateCellEditor());
    dataPartidaModel.setCellRenderer(new DateCellRenderer());
    dataRetornoModel.setCellRenderer(new DateCellRenderer());

}

In the case the idea would be the following, there would be a button that, when clicking, would add an empty line, for user editing, I can include this line normally:

HoweverifIalreadyincludevaluesitdoesnotshow,evenifIedit,nothinghappens.Thecallthatfillsheristhis:

privatevoidencheTable(){PassagemAereapa=newPassagemAerea();pa.setAeroportoRetorno("TESTE");
    pa.setAeroportoSaida("TESTE");
    pa.setDataPartida(Calendar.getInstance().getTime());
    pa.setDataVolta(Calendar.getInstance().getTime());
    pa.setTipoBagagem("TESTE");
    pa.setHorarioChegada("10");
    pa.setHorarioSaida("TESTE");

    passagemTableModel.adicionaPassagem(pa);
}

How to solve?

    
asked by anonymous 30.10.2017 / 04:46

1 answer

1

The method responsible for displaying the data in the table cells is getValueAt " inherited from the interface TableModel , and if you observe its signature ( Object getValueAt​(int rowIndex, int columnIndex) ) you will see that it returns Object . This return is the information that will be displayed in each cell of the table, because internally java calls this method several times according to the number of rows and columns that the table has.

Your getValueAt method retrieves values from the Passagem object, but nothing does with it, and in the end it is always returning null , because in none of the cases do you return information from the objects to the table. By adding a return to each case, the cells will be displayed correctly:

@Override
public Object getValueAt(int rowIndex, int columnIndex) {

    switch(columnIndex)
    {
        case 0: return passagem.get(rowIndex).getDataPartida();
        case 1: return passagem.get(rowIndex).getDataVolta();
        case 2: return passagem.get(rowIndex).getAeroportoSaida();
        case 3: return passagem.get(rowIndex).getAeroportoRetorno();
        case 4: return passagem.get(rowIndex).getHorarioChegada();
        case 5: return passagem.get(rowIndex).getHorarioSaida();
        case 6: return passagem.get(rowIndex).getTipoBagagem();
        default: return null;
    }
}
    
30.10.2017 / 10:28