How to refresh a JTable model after insert delete or update the data

0 votes

I have the following JTable code:

private JTable fetchJTable() {
    String[] col = { "Name", "Email", "Contact No. 1", "Contact No. 2",
            "Group", "" };
    if (jTable == null) {
        jTable = new JTable() {
            public boolean isCellEditable(int nRow, int nCol) {
                return false;
            }
        };
    }
    DefaultTableModel contactTableModel = (DefaultTableModel) jTable
            .getModel();
    contactTableModel.setColumnIdentifiers(col);
    jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    return jTable;
}

I will call the  fetchJTable() method to retrieve the data from the database and insert it into a 

public void setUpTableData() {
    DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();
    ArrayList<Contact> list = new ArrayList<Contact>();
    if (!con.equals(""))
        list = sql.getContactListsByGroup(con);
    else
        list = sql.getContactLists();
    for (int i = 0; i < list.size(); i++) {
        String[] data = new String[7];

            data[0] = list.get(i).getName();
            data[1] = list.get(i).getEmail();
            data[2] = list.get(i).getPhone1();
            data[3] = list.get(i).getPhone2();
            data[4] = list.get(i).getGroup();
            data[5] = list.get(i).getId();

        tableModel.addRow(data);
    }
    jTable.setModel(tableModel);
}

With this code, I was using the fetchJTable() method to refresh the table once the data has been inserted and updated. But before refreshing the data, I am trying to clear the table before restructuring the table model:

DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel(); tableModel.setRowCount(0);

Is this the correct way or are there any other standard ways?

Oct 11, 2018 in Java by 93.lynn
• 1,600 points
23,902 views

1 answer to this question.

0 votes

In my opinion, you can notify your JTable about the data change by simply using tableModel.fireTableDataChanged().

For more details, you can refer the documentation:

Notifies all listeners that all cell values in the table's rows may have changed. The number of rows may also have changed and the JTable should redraw the table from scratch. The structure of the table (as in the order of the columns) is assumed to be the same.

answered Oct 11, 2018 by code.reaper12
• 3,500 points
This comment is worthless.
hahahahaha xD

Related Questions In Java

0 votes
1 answer
0 votes
3 answers

How to check whether a file exists or not in Java?

Using nio we can check whether file ...READ MORE

answered Aug 14, 2018 in Java by Sushmita
• 6,910 points
3,576 views
0 votes
3 answers

How to check whether a string is empty or not? Is there a function for this?

str != null && str.length() != 0 alternatively str ...READ MORE

answered Sep 11, 2018 in Java by Sushmita
• 6,910 points
1,009 views
0 votes
2 answers

How can I get the filenames of all files in a folder which may or may not contain duplicates

List<String> results = new ArrayList<String>(); File[] files = ...READ MORE

answered Sep 12, 2018 in Java by Sushmita
• 6,910 points
1,643 views
0 votes
1 answer

How to add row in JTable

To create a table: JTable table = new ...READ MORE

answered Jun 29, 2018 in Java by Akrati
• 960 points
2,705 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 79,501 views
+1 vote
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,500 points
7,954 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points
16,987 views
0 votes
1 answer

How to remove ‘char’ from a String?

Hi...here you can try using the overloaded ...READ MORE

answered May 22, 2018 in Java by code.reaper12
• 3,500 points
516 views
0 votes
1 answer

How to download a file from spring controllers?

@RequestMapping(value = "/files/{file_name}", method = RequestMethod.GET) public void ...READ MORE

answered Sep 4, 2018 in Java by code.reaper12
• 3,500 points
2,603 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP