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:
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?
It worked, thank you very much !! Solved.
– Jose Aguiar
@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 :)– user28595