添加新行后,JTable不显示新模型

时间:2012-08-27 22:30:28

标签: java swing netbeans jtable defaulttablemodel

我无法重画我的jTable。我必须重新运行所有应用程序以显示addRow。

这是代码:

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);

这最后3个println()表明模型已经改变了!     大声笑{} 输出:

Tratta AGGIUNTA

javax.swing.table.DefaultTableModel@1ca7e4a

BOLOGNA

...

卡坦札罗

NEW_DEPARTURE_ADDED

javax.swing.table.DefaultTableModel@17​​d54dd

javax.swing.table.DefaultTableModel@17​​d54dd

所以它有效..但它没有显示新模型!!

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 个答案:

答案 0 :(得分:2)

您需要通知表格,结构和/或数据已更改。问题在这里得到解答,例如:JTable How to refresh table model after insert delete or update the data.