Data is not displayed in the table by Abstracttablemodel

Asked

Viewed 155 times

2

I am developing a ticket request system, but for having to add a JXDataPicker in the table, I had to implement the AbstractTableModel.

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

This is the model 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 call of the model in the constructor of the class containing 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 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:

inserir a descrição da imagem aqui

but if I already include values it does not show, even if I edit, nothing happens. The call that fills it is this:

 private void encheTable(){
    PassagemAerea pa = new PassagemAerea();

    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?

1 answer

1


The method responsible for displaying the data in the table cells is the getValueAt inherited from the interface TableModel, and if you observe his signature(Object getValueAt​(int rowIndex, int columnIndex)) you’ll see he 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.

His method getValueAt recovers values of the object Passagem, but does nothing with it, and in the end is always returning null, because in none of the cases do you return information from the objects to the table. Adding a return in each case, 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;
    }
}
  • It worked, thank you very much !! Solved.

  • @Joseaguiar you can accept the answer by clicking on v beside, thus, the question is as solved and serves as a reference to others with similar problem :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.