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?