fireTableRowsUpdated()在JTable中完成更新后不起作用 [英] fireTableRowsUpdated() Not work after update done in JTable

查看:190
本文介绍了fireTableRowsUpdated()在JTable中完成更新后不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的JTable中,更新完成后,需要刷新以显示更改:

In my JTable, after update done, it need to refresh to show the changes:

public class RecordTableGUI extends JFrame implements ActionListener {
private String newName;
private JTable table;
private RecordTableModel myModel;
private JButton editButton;

    public RecordTableGUI() {
    myModel = new RecordTableModel();
    table = new JTable(myModel);

    add(new JScrollPane(table), BorderLayout.CENTER);
    add(buttonsPanel(), BorderLayout.SOUTH);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(700, 550);
    setLocation(300, 80);
    setVisible(true);
}

public JPanel buttonsPanel() {
    JPanel bPanel = new JPanel();
    editButton = new JButton("Edit");
    editButton.addActionListener(this);
    bPanel.add(editButton);
    return bPanel;
}

@Override
public void actionPerformed(ActionEvent e) {
        if (table.getSelectedRow() > -1) {
        Object oldName = table.getValueAt(table.getSelectedRow(), 1);

        UpdateGUIDialog updDialog = new UpdateGUIDialog(this,String.valueOf(oldName), this);

        int rowToEdit = table.getSelectedRow();
        int rowToModel = table.convertRowIndexToView(rowToEdit);
        Object nameID = table.getValueAt(table.getSelectedRow(), 0);
        myModel.updateRow(rowToModel, nameID, getNewName());
    } else {
        JOptionPane.showMessageDialog(null, "Select a row");
    }
}
}

模型类:

public class RecordTableModel extends AbstractTableModel {
Connection con;
Statement statement;
ResultSet result;
String dbUrl = "jdbc:mysql://localhost/mydb";
String query = "Select * from mytable";
ArrayList<String> cols = new ArrayList<String>();
ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();

    public RecordTableModel() {
    try {
        con = DriverManager.getConnection(dbUrl, "root", "2323");
        statement = con.createStatement();
        result = statement.executeQuery(query);

        int c = result.getMetaData().getColumnCount();
        for (int i = 1; i <= c; i++) {
            cols.add(result.getMetaData().getColumnName(i));
        }

        while (result.next()) {

            ArrayList<String> eachRow = new ArrayList<String>();
            for (int i = 1; i <= c; i++) {
                eachRow.add(result.getString(i));
            }
            data.add(eachRow);
        }

    } catch (SQLException sqle) {
        sqle.printStackTrace();
    } finally {
        try {
            if (con != null) {
                con.close();
            }
            if (statement != null) {
                statement.close();
            }
        } catch (SQLException sqlee) {
            sqlee.printStackTrace();
        }
    }
}
    @Override
public int getRowCount() {
    return data.size();
}

@Override
public int getColumnCount() {
    return cols.size();
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
    ArrayList<String> selectedRow = data.get(rowIndex);
    return selectedRow.get(columnIndex);
}

@Override
public String getColumnName(int col) {
    return cols.get(col);
}
    public void updateRow(int modelRow, Object nameID, Object newName) {
        String query = "update mytable set name = '" + newName + "' where id = " + nameID;
    Connection conn;
    PreparedStatement pstate;
    try {
        conn = DriverManager.getConnection(dbUrl, "root", "2323");
        pstate = conn.prepareStatement(query);
        pstate.executeUpdate();
        fireTableRowsUpdated(tableRow, tableRow);
        fireTableDataChanged();
        fireTableCellUpdated(modelRow, 1);
    } catch (SQLException sql) {
        sql.printStackTrace();
    }
}


推荐答案

这里是您的原始代码:

public class RecordTableModel extends AbstractTableModel {
...
    public void updateRow(int modelRow,...) {
    String query = ...;
    Connection conn;
    PreparedStatement pstate;
    try {
        conn = DriverManager.getConnection(...);
        pstate = conn.prepareStatement(query);
        pstate.executeUpdate();
        fireTableRowsUpdated(modelRow, modelRow);     // Not Work!
        fireTableDataChanged();                       // Not Work!
        fireTableCellUpdated(modelRow, 1);           // Not Work!
    } catch (SQLException sql) {
        sql.printStackTrace();
    }
}

此代码对表格所持有的数据不执行任何操作模型本身,所以调用 fireTableXXX(...)什么也不做也就不足为奇了。如果模型没有改变,你可以触发你想要的任何东西,并且表格不会改变。

This code does nothing to the data held by the table model itself, and so it should coe as no surprise that calling fireTableXXX(...) does nothing. If the model is unchanged, you can fire anything you want, and the table won't change.

你可能不应该使用executeUpdate而是executeQuery这样你就可以得到一个从数据库中获取ResultSet,然后使用它来更新表模型所持有的数据。 然后调用相应的 fireTableXXX(...)方法。

You should perhaps not be using executeUpdate but executeQuery so you can get a ResultSet from the database, and then use it to update the data held by your table model. Then call your appropriate fireTableXXX(...) method.

这篇关于fireTableRowsUpdated()在JTable中完成更新后不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆