-1

I have a JTable that uses a DefaultTableModel, I got the result in the array but after the execution of the for loop, int i will always print zero value. The loop is working fine.

This is the result

Ramalan forecast = model.ramalan(12);
DefaultTableModel tb = new DefaultTableModel();
String header[] = new String[] { "No", "tgl", "harga", "Batas Atas", "Batas Bawah" };
tb.setColumnIdentifiers(header);
Vector<Object> rml = new Vector<>();
for(int i=0; i<forecast.estimasiPoin().ukuran(); i++){
    rml.add(i);
    rml.add("");
    rml.add(aturDesim.format(forecast.estimasiPoin().pada(i)));
    rml.add(aturDesim.format(forecast.intervalPrediksiAtas().pada(i)));
    rml.add(aturDesim.format(forecast.intervalPrediksiAtas().pada(i)));
     tb.addRow(rml);
}
tableRamal.setModel(tb);

Here's how it's supposed to print the values:

for(int i=0; i<12; i++){
    rml.add(i);
    rml.add("");
    rml.add(aturDesim.format(forecast.estimasiPoin().pada(i)));
    rml.add(aturDesim.format(forecast.intervalPrediksiAtas().pada(i)));
    rml.add(aturDesim.format(forecast.intervalPrediksiAtas().pada(i)));
     tb.addRow(rml);
}
tableRamal.setModel(tb);

but it is still returning zero.

yusuf muhammad
  • 53
  • 1
  • 10

1 Answers1

2

You are re-using the same vector and just adding more elements on to the end.

Change your code to this:

for(int i=0; i<forecast.estimasiPoin().ukuran(); i++){
    Vector<Object> rml = new Vector<>();
    rml.add(i);
    rml.add("");
    rml.add(aturDesim.format(forecast.estimasiPoin().pada(i)));
    rml.add(aturDesim.format(forecast.intervalPrediksiAtas().pada(i)));
    rml.add(aturDesim.format(forecast.intervalPrediksiAtas().pada(i)));
    tb.addRow(rml);
}
xtratic
  • 4,600
  • 2
  • 14
  • 32