Chris J
Chris J

Reputation: 1447

Java: Refreshing my jTable after data updates in child jDialog

I've investigated lots of different questions and answers around this, but can't find one that seems to work.

I'm new to Java, but have experience in a variety of different languages and, so far (in context to what I'm experimenting with), it's feeling a bit like VBA except with having to build up the actions/functions that you feel should be already there. This is, I expect, just down to my own inexperience though.

I'm using Netbeans IDE 8.2 with Java Runtime 1.80.

I have created jFrame that contains a jTable. The jTable is built with data like so:

public void showTasks(Boolean removeID) {
    ArrayList<Tasks> list = tasksList("SELECT * FROM tasks");
    JTable table = jTable1;
    DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
    Object[] row = new Object[4];
    for(int i=0;i<list.size();i++) {
        row[0]=list.get(i).getId();
        row[1]=list.get(i).getName();
        row[2]=list.get(i).getDesc();
        row[3]=list.get(i).getDate();
        model.addRow(row);
    }
    // Remove the 'id' column from the table view 
    if(removeID) {    table.removeColumn(table.getColumnModel().getColumn(0)); }
}

The background behind this is less relevant, but essentially tasksList is a function that applies the query to an SQL statement, returning an ArrayList. I build up my rows with 4 columns, then remove the first column so 'ID' is available but not visible (this final action has been segregated through testing/experimentation).

I have another area of code that opens a jDialog when a row is clicked, in which it is possible to update the MySQL DB.

Problem

I'm trying to throw in a function call so that the table data 'refreshes' when the jDialog is closed. I have temporarily added in a button to the jFrame (where the jTable lives) to test/debug this function.

I can't seem to get this to work, though. The closest I have achieved is to re-call showTasks(false), but this obvious just adds rows with updated data, rather than replacing the dataset. I'm not 100% sure if deleting all the rows, then building them back in is 'best practice'.

As I'm new to Java, and may still be looking at it from a flawed method of thinking, I'm finding it difficult to apply any other examples to that of my own. I also can't seem to find a way to implement fireTableDataChanged().

Surely this is a simple concept I'm over-thinking?

Edit - based on below answer

Is there a reason why something like this would be considered incorrect, if deleting all rows and adding them back in is okay?

public void refreshTasks() {
    DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
    int row_total = model.getRowCount();
    for(int i= row_total -1;i>=0;i--) {
        model.removeRow(i);
    }
 showTasks(false);
}

Edit: Button to invoke data update

Now works correctly (if not improperly) with the following:

private DefaultTableModel parentTable;      // To store the parent 'Task' table model

public void setStart(int user,DefaultTableModel table) {

  this.txt_taskID.setText(Integer.toString(user)); // Converts the ID to a string
  addData(user);          // Populates the fields
  parentTable = table;    // Sets parent TableModel to a variable

}

The above code is called from the Parent jFrame when the Dialog is opened, and passes the Table model and the 'ID' of the row I'm looking to edit. The table model is stored in parentTable.

There's also a 'Save' button, and a 'Cancel' button. I'm yet to separate these, and currently 'Save' does just that (SQL update and so on). My 'Cancel' button closes the dialog and refreshes the jTable, as per the below function:

private void btn_CancelActionPerformed(java.awt.event.ActionEvent evt) { 

    this.setVisible(false);            // Hide the dialog
    Menu menu = new Menu();            // for accessing the tasksList function

    parentTable.setRowCount(0);        // Clears the jTable data

    // jTable data is then 'rebuilt' using the new data

    ArrayList<Tasks> list = menu.tasksList("SELECT * FROM tasks");
    Object[] row = new Object[4];
    for(int i=0;i<list.size();i++) {
        row[0]=list.get(i).getId();
        row[1]=list.get(i).getName();
        row[2]=list.get(i).getDesc();
        row[3]=list.get(i).getDate();
        parentTable.addRow(row);
    }
} 

Upvotes: 2

Views: 1136

Answers (2)

camickr
camickr

Reputation: 324147

I'm not 100% sure if deleting all the rows, then building them back in is 'best practice'.

Yes that is probably the best practice.

The only other approach is to create a completely new TableModel and add it to the table using the setModel() method. The problem with this approach is that it will reset any custom renderers/editors you may have set on the table.

The easiest way to remove all the rows from the DefaultTableModel is to just use:

model.setRowCount(0);

Upvotes: 2

Coder ACJHP
Coder ACJHP

Reputation: 2224

I'm not sure how you want to do it but I'm gonna give you simple example for deleting and refreshing JTable maybe it's help you.

This following btnDelete Jbutton added to JFrame for deleting rows from table:

btnDelete.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int rowIndex = table.getSelectedRow();
            if(rowIndex > -1) {
                int x = (Integer) table.getModel().getValueAt(rowIndex, 0);
                if (conn.removeContact(x) == true) { //here add your own code like removeID

                    model.removeRow(rowIndex);
                    lblInfo.setText("Contact deleted successfully.");
                    model.fireTableDataChanged();
                } else {
                    lblInfo.setText("Cannot remove at this time!");
                }
            }else {
                lblInfo.setText("At first you need select a row with single click!");
                return;
            }
        }
    });

and these following codes for refreshing table in primitive way :

btnRefresh.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            int rowCount = model.getRowCount();
            for (int i = rowCount - 1; i >= 0; i--) {//remove all rows

                model.removeRow(i);
            }
            lblInfo.setText("Table datas updated successfully.");

            for (Person p : conn.readAllContacts()) {//add all row from scratch
                model.addRow(new Object[] { p.getId(), p.getName(), p.getLastName(), p.getPhone(), p.getEmail() });
            }

        }
    });

Upvotes: -1

Related Questions