-1

I try to select certain row from jTable and perform a deletion then the jTable will be updated with the latest data in database. This is how I set up jTable :

    private JTable getJTableManageReplies() {
    jTableManageReplies.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    jTableManageReplies.getSelectionModel().addListSelectionListener(
            new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent e) {
                    if (!e.getValueIsAdjusting()) {
                        int viewRow = jTableManageReplies.getSelectedRow();
                        // Get the first column data of the selectedrow
                        int replyID = Integer.parseInt(jTableManageReplies.getValueAt(
                                viewRow, 0).toString());

                        eForumRepliesAdmin reply = new eForumRepliesAdmin(replyID);
                        replyID = JOptionPane.showConfirmDialog(null, "Are you sure that you want to delete the selected reply? " , "Delete replies", JOptionPane.YES_NO_OPTION);
                        if(replyID == JOptionPane.YES_OPTION){
                        reply.deleteReply();
                        SetUpJTableManageReplies();}
                    }
                }
            });
    return jTableManageReplies;
}

     public void SetUpJTableManageReplies() {

    DefaultTableModel tableModel = (DefaultTableModel) jTableManageReplies
            .getModel();
    String[] data = new String[5];
    db.setUp("IT Innovation Project");
    String sql = "Select forumReplies.reply_ID,forumReplies.reply_topic,forumTopics.topic_title,forumReplies.reply_content,forumReplies.reply_by from forumReplies,forumTopics WHERE forumReplies.reply_topic = forumTopics.topic_id ";
    ResultSet resultSet = null;
    resultSet = db.readRequest(sql);
    tableModel.getDataVector().removeAllElements();

    try {
        while (resultSet.next()) {
            data[0] = resultSet.getString("reply_ID");
            data[1] = resultSet.getString("reply_topic");
            data[2] = resultSet.getString("topic_title");
            data[3] = resultSet.getString("reply_content");
            data[4] = resultSet.getString("reply_by");
            // Add data to table model
            tableModel.addRow(data);
        }
        resultSet.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}

And this is my codes to perform deletion from database :

    public boolean deleteReply() {
    boolean success = false;
    DBController db = new DBController();
    db.setUp("IT Innovation Project");
    String sql = "DELETE FROM forumReplies where reply_ID = " + replyID
            + "";
    if (db.updateRequest(sql) == 1)
        success = true;
    db.terminate();
    return success;
}

However, there is an error message which is ArrayIndexOutOfBound right after I add the SetUpJTableManageReplies methos in the jDialog box. I try to do like when user select certain row, there will be a pop out to ask for confirmation of deletion. Then right after they click on yes, the jTable data will be refreshed. Can somebody give me some guides? Thanks in advance.

Newbies
  • 13
  • 5
  • 1
    Can you post the stacktrace from the `ArrayIndexOutOfBound` exception? – Andreas Fester Jan 18 '13 at 14:32
  • java.lang.ArrayIndexOutOfBoundsException: 9 >= 1. I currently have 9 rows of data in database table right after I perform the deletion. – Newbies Jan 18 '13 at 14:38
  • Anybody know whats the problem? – Newbies Jan 18 '13 at 14:51
  • 1
    Post an [SSCCE](http://sscce.org). Showing us only bits and pieces of code makes it almost impossible for us to help you. I believe that this is not the first time I am giving you this advice: http://stackoverflow.com/questions/14334905/jtable-is-not-refreshing-the-data – Guillaume Polet Jan 18 '13 at 15:35

2 Answers2

1

Your Problem is here:

tableModel.getDataVector().removeAllElements();

Better:

tableModel.setRowCount(0);

Much better: write your own table model and implement all methods which are defined in TableModel interface - so you can learn how to deal with the JTable component

Sergiy Medvynskyy
  • 11,160
  • 1
  • 32
  • 48
  • If I change to this then I get another error message : Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1 – Newbies Jan 19 '13 at 02:25
  • I managed to do the refresh of jTable by adding jTableManageReplies.repaint(); But the existing posts will keep on re-add. When I add the setRowCount method to remove the existing posts, the error comes again. Any guides to fix this? – Newbies Jan 19 '13 at 03:28
  • Please post the complete stack trace. Do not forget, that after deleteReply you must call SetUpJTableManageReplies – Sergiy Medvynskyy Jan 21 '13 at 11:47
0

Use TableModel to manage table data. DefaultTableModel will be useful, you should first create tableModel, then create JTable and set table's model to previously created table model.

You should perform insert/delete/update to table cells using model, which will update JTable automatically. Use DefaultTableModel to manage your data.

nullptr
  • 3,320
  • 7
  • 35
  • 68