1
I’m using java swing where I have a window and in it a JTable
.
I run a method where you will read the data from this Jtable
and update a column, cell by cell, of this Jtable
. The problem is that it runs the method normally by running the looping, but updates the column(all cells) only at the end. I would like you to update the cell line by line and not everything only at the end.
Follows code:
private void BtnEnviarActionPerformed(java.awt.event.ActionEvent evt) {
SMSMultiploBean smsb = new SMSMultiploBean();
SMSMultiploDAO smsd = new SMSMultiploDAO();
int i;
int linha = 0;
int totenviados = 0;
for(i = 0;i<TblDadosSMS.getModel().getRowCount();i++){
smsb.setDdd((String) TblDadosSMS.getValueAt(linha, 4));
smsb.setTelefone((String) TblDadosSMS.getValueAt(linha, 5));
smsb.setMensagem((String) TblDadosSMS.getValueAt(linha, 6));
smsb.setLinha((int)TblDadosSMS.getValueAt(linha,8));
Date data= JData.getDate();
Format ff = new SimpleDateFormat("ddMMyyyy");
String datastring = ff.format(data);
smsb.setData(datastring);
try {
String retorno = smsd.EnviaSms(smsb);
//retorno falando se o SMS foi enviado ou não
TblDadosSMS.setValueAt(retorno, i, 7);
//TblDadosSMS.firePropertyChange(retorno, i, i);
Pbar.setMinimum(0);
Pbar.setMaximum(Integer.parseInt((LblTotal.getText())));
Pbar.setValue(i+1);
if (retorno.equals("Enviado")){
totenviados += 1;
LblEnviados.setText(Integer.toString(totenviados));
}
} catch (Exception ex) {
Logger.getLogger(FrmSMSSimultaneo.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(this, "Erro: "+ex);
}
linha++;
}
}
Window before executing method:
Window after executing code:
Below is code for popular table:
private void CarregaDadosPlanilha() throws SQLException, IOException, ParseException, Exception {
ExcelBean eb = new ExcelBean();
ExcelDao ed = new ExcelDao();
//data convertida para String
Date data = JData.getDate();
SimpleDateFormat formatador = new SimpleDateFormat("dd/MM/yyyy");
String novadata = formatador.format(data);
//conversão String para date
String texto = novadata;
String formato = "dd/MM/yyyy";
Date dataobjeto = new SimpleDateFormat(formato).parse(texto);
//System.out.println("Data tratada date: "+dataobjeto);
eb.setData(dataobjeto);
List<ExcelBean> ListaDadosPlanilha = new ArrayList<ExcelBean>();
// ListaDadosPlanilha = ed.CarregaExcel(eb);
ListaDadosPlanilha = ed.LeExcel(eb);
DefaultTableModel tbm = (DefaultTableModel) TblDadosSMS.getModel();
//zera as linhas da tabela
for (int i = tbm.getRowCount() - 1; i >= 0; i--) {
tbm.removeRow(i);
}
int i = TblDadosSMS.getRowCount();
/*
* preenche a tabela
*/
for (ExcelBean eb2 : ListaDadosPlanilha) {
tbm.addRow(new String[1]);
TblDadosSMS.setValueAt(eb2.getNome(), i, 0);
TblDadosSMS.setValueAt(eb2.getServico(), i, 1);
//data convertida para String
Date dataexcel = JData.getDate();
SimpleDateFormat f = new SimpleDateFormat("dd/MM/yyyy");
String datastring = f.format(dataexcel);
TblDadosSMS.setValueAt(datastring, i, 2);
TblDadosSMS.setValueAt(eb2.getHorario(), i, 3);
TblDadosSMS.setValueAt(eb2.getDdd(), i, 4);
TblDadosSMS.setValueAt(eb2.getTelefone(), i, 5);
eb2.setMensagem("Prezado(a): "+eb2.getNome()+
" , nao se esqueca que tem o servico: "+eb2.getServico()+" marcado para: "+novadata+
" as: "+eb2.getHorario());
TblDadosSMS.setValueAt(eb2.getMensagem(), i, 6);
TblDadosSMS.setValueAt(eb2.getLinha(),i,8);
System.out.println("Linha célula form: "+eb2.getLinha());
//System.out.println("linha jtable: "+eb2.getLinha());
i++;
}
LblTotal.setText(Integer.toString(tbm.getRowCount()));
}
You are using your own Tablemodel or are using defaultTableModel in this table?
– user28595
Rodrigo, when you create a question and need to supplement it, click [Edit] on the question and directly add the information to it. The field below is for answers.
– user28595
Sorry Diego, I’m still getting the hang of it.
– Rodrigo Storti de Oliveira
Sorry for the intrusion, but you have already tried to use a method that sends one by one, reading this one after the change and let automatically change (update) a second method and so on until you finish all?
– Douglas