ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – fireTableRowsUpdated()在JTable中完成更新后无效

2019-10-03 04:03:35  阅读:159  来源: 互联网

标签:java swing jtable


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

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(…)什么都不做也就不足为奇了.如果模型未更改,您可以触发任何所需的内容,并且表格不会更改.

您可能不应该使用executeUpdate而是executeQuery,这样您就可以从数据库中获取ResultSet,然后使用它来更新表模型所拥有的数据.然后调用适当的fireTableXXX(…)方法.

标签:java,swing,jtable
来源: https://codeday.me/bug/20191003/1846778.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有