0

I try to refresh the data of jTable upon deletion of selected row. Here are my codes to set up table :

    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();
                        JOptionPane.showMessageDialog(null, "Reply has been deleted successfully.");
                        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);

    jTableManageReplies.repaint();
    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");
            tableModel.addRow(data);
        }
        resultSet.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}

And this is my sql statement :

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

I called the repaint() to update the table data with the newest data in database and it works. I mean the data after deletion of certain row. However, the existing posts will keep on re-add. Then I add the removeAllElement method to remove all the existing posts because my sql statement is select * from table. Then, there is an error message which is ArrayIndexOutOfBoundsException. Any guides to fix this? Thanks in advance.

Newbies
  • 13
  • 5
  • Post SSCCE? Which means **Short, Self Contained, Correct (Compilable), Example** And your code is no were near to this. Please remove all the unwanted codes for us and post simple code that shows the problem. – Amarnath Jan 19 '13 at 04:59
  • I edited already. I can use the repaint method to update the table data. However, I want to delete the existing posts because they will keep on adding. But when I call the removeAllElement method, the arrayIndexOutOfBoundException occurs. I even tried the setRowCount(0) method and it's the same. – Newbies Jan 19 '13 at 05:03

1 Answers1

1

I called the repaint() to update the table data with the newest data in database and it works.

There is no need to call repaint method when data is changed. Data change is handled by the Table Model (DefaultTableModel in this case.) And fireXXXMethods are required to be called whenever data is changed but you are using DefaultTableModel even those are not required. (Since by default it call these methods when ever there is a change.)

I think the problem is in the valuesChanged(..) method. You are getting the value at row 0 but not checking whether table has rows or not. So keep a constraint.

int viewRow = jTableManageReplies.getSelectedRow();
// Get the first column data of the selectedrow
if(jTableManageReplies.getRowCount() > 0)
    int replyID = Integer.parseInt(jTableManageReplies.getValueAt(viewRow, 0).toString());
Amarnath
  • 8,736
  • 10
  • 54
  • 81
  • Ya it works now! Thanks a lot. So the fireTableDataChanged method serves the purpose of notify the table whenever there is any data changed? Is it correct? – Newbies Jan 19 '13 at 05:14
  • Absolutely correct. Any change in data is handled by the _Table Model._ And one more thing **repaint()** is required if there is a change in components that is adding or deleting components. So use it appropriately. Have a great weekend .. :-) – Amarnath Jan 19 '13 at 05:23
  • 1
    Use `validate()` when adding or deleting components; use `repaint()` when changing unbound properties. +1 for identifying the need to `repaint()` as a problem to be solved in the model. – trashgod Jan 19 '13 at 09:20