3

I can't repaint my jTable. I must re-run all application to show addRow.

This is the code:

MainPage.java http://pastebin.com/6FZL1pKC

public class Mainpage2 extends javax.swing.JFrame {

public static DefaultTableModel tripModel;
/**
 * Creates new form carpoolingui
 */
public Mainpage2(){
    try {
        tripModel = MPtable.myModel(Parser.parsa_tratta(Database.doRequest("SELECT * from tratte")));
    } catch (Exception ex) {
        Logger.getLogger(Mainpage2.class.getName()).log(Level.SEVERE, null, ex);
    }
    initComponents();
}
....
jTable1.setModel(tripModel);
....
public void tripRepaint() throws MalformedURLException, Exception{
        System.out.println(jTable1.getModel());
        tripModel = MPtable.myModel(Parser.parsa_tratta(Database.doRequest("SELECT * from tratte")));
        jTable1.setModel(tripModel);
        jTable1.repaint();

        System.out.println(jTable1.getModel());
        System.out.println(tripModel);

These last 3 println() show that model has changed! lol{} Output:

Tratta AGGIUNTA

javax.swing.table.DefaultTableModel@1ca7e4a

BOLOGNA

...

CATANZARO

NEW_DEPARTURE_ADDED

javax.swing.table.DefaultTableModel@17d54dd

javax.swing.table.DefaultTableModel@17d54dd

So it works..but It doesn't show new model!!

AddTrip.java http://pastebin.com/t0LqF91a

Carpooling2.mainPage2.tripRepaint();

MPTable.java http://pastebin.com/2gXvYNLC

 public class MPtable {


public static DefaultTableModel myModel(Tratta[] tratta_array){
    int i;

    DefaultTableModel model = new DefaultTableModel();
    model.addColumn("ID");
    model.addColumn("LUOGO PARTENZA");
    model.addColumn("LUOGO ARRIVO");
    model.addColumn("DATA");
    model.addColumn("ORA PARTENZA");
    model.addColumn("ORA ARRIVO");
    model.addColumn("COSTO (€)");
    model.addColumn("POSTI DISPONIBILI");
    model.addColumn("AUTISTA");
    model.addColumn("INFORMAZIONI");

    for (i=0; i<tratta_array.length; i++){
            System.out.println(tratta_array[i].getLuogoPartenza());
            }

    for (i=0; i<tratta_array.length; i++){
        model.addRow(new Object[] {
                tratta_array[i].getId(),
                tratta_array[i].getLuogoPartenza(),
                tratta_array[i].getLuogoArrivo(),
                tratta_array[i].getData(),
                tratta_array[i].getOraPartenza(),
                tratta_array[i].getOraArrivo(),
                tratta_array[i].getCosto(),
                tratta_array[i].getPostiDisponibili(),
                tratta_array[i].getAutista(),
                tratta_array[i].getInfo()
                }
        );
    }
    return model;
    }

1 Answer 1

2

You need to inform the table, that the structure and/or data has changed. Question is answered here e.g: JTable How to refresh table model after insert delete or update the data.

1
  • I've red it, but I've tried to add that after MPTable.myModel() or jTable1.setModel or .repaint() [all in tripRepaint()] with no success
    – Gorgo
    Aug 27, 2012 at 23:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.