Results 1 to 5 of 5
  1. #1
    muhindi is offline Member
    Join Date
    Jan 2011
    Rep Power

    Default Updating a JTable after Inserting Record

    Greeetings All,
    Iam developing a swing application with a derby database and using Toplink JPA for persistence. My issue is that the JTable will not update to reflect the inserted record.

    For the table, I have created a tablemodel that extends DefaultTableModel and implements TableModelListener and in the tableChanged method of the class I've tried a variety of update methods e.g validate(), revalidate(), and SwingUtilities.updateComponentTreeUI(comp).None seems to work.:(

    I've called the fireTableDataChanged() method after adding my record . I am out of ideas on the issue. I could post some snippets of the code I have if need be.

    Last edited by muhindi; 01-19-2011 at 02:03 PM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Rep Power


    Is the insert into the DB reflected in the data model that backs the JTable? I suggest posting an SSCCE that demonstrates the problem to clarify

  3. #3
    muhindi is offline Member
    Join Date
    Jan 2011
    Rep Power


    Pardon for the delay in reply. Yes the database reflects the inserted record but only
    after I run the application again is the new record shown.
    The custom table model I created is

    PHP Code:
    // class AppModel : The Customized tableModel to 
        // facilitate updating of the JTable after records are inserted
        class AppModel extends DefaultTableModel implements TableModelListener{
            public AppModel(){
                System.out.println("AppModel created");
            public void tableChanged(TableModelEvent e) {
                 		tblStores.validate();//tried this did'nt work
                		System.out.println(" New Record Added ");
    In the Netbeans form designer I used post creation code tab on the properties window to attach my
    custom tableModel to the Jtable and also set the Listener as below:

    PHP Code:
    In the addStore button I added the following code:
    PHP Code:
    Stores s=new Stores(txStoreName.getText(),txBuilding.getText(),
                if(dbworker.addStore(s))//after successful insert to database...
                         appmodel.fireTableDataChanged();// call tableChanged() in custom table model
    The tableDataChanged method is being executed since I can see the "New record Added "statement
    on the console. But the JTable just wont refresh.
    I hope these snippets will help in clarification of the problem.
    Last edited by muhindi; 01-20-2011 at 09:02 AM.

  4. #4
    kiruba is offline Member
    Join Date
    Jan 2011
    Rep Power


    i need help......

    tell me how can i remove Table header...
    i tried columnRemove method .it removed the column but when ever i add new column using model.addColumn("new Field"); but what happened here old header and new header are append...
    what i need means i need only the new header only...
    and this is my code:::::

    package sample;

    import javax.swing.*;
    import javax.swing.table.*;
    import java.awt.*;

    public class TableColumnRemove {

    DefaultTableModel model;
    JTable table;

    public static void main(String[] args) {
    new TableColumnRemove();

    public TableColumnRemove() {
    JFrame frame = new JFrame("Remove a column from a JTable");
    JPanel panel = new JPanel();
    String data[][] = {{"Vinod", "MCA", "Computer"},
    {"Deepak", "PGDCA", "History"},
    {"Ranjan", "M.SC.", "Biology"},
    {"Radha", "BCA", "Computer"}};
    String col[] = {"Name.", "Course", "Subject"};
    model = new DefaultTableModel(data, col);
    table = new JTable(model);
    JTableHeader header = table.getTableHeader();
    JScrollPane pane = new JScrollPane(table);
    frame.setSize(500, 150);
    frame.getRootPane().setWindowDecorationStyle(JRoot Pane.PLAIN_DIALOG);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);

    int columnCount = table.getColumnCount();
    for (int i = 0; i < columnCount; i = 0) { // here im remove columns
    Remove(table, i);
    columnCount = table.getColumnCount();
    model.addColumn("new Field"); //after add new column


    private void Remove(JTable table, int col_index) {
    TableColumn tcol = table.getColumnModel().getColumn(col_index);


  5. #5
    muhindi is offline Member
    Join Date
    Jan 2011
    Rep Power


    SOLVED AT LAST!! Worked on it for 3 nights. Solution ?.

    1. I set the List holding the query resultset to observable.
    2. In the eventhandler responsible for persisting the entity, I added the following single line of code:
    PHP Code:
    This will cause the change(insert or delete) to be 'viewable' on the JTable.

    Hope this helps someone else.;)

Similar Threads

  1. problem with inserting new record
    By gerard kowara in forum JDBC
    Replies: 1
    Last Post: 10-26-2010, 11:33 AM
  2. Updating the existing JTable !
    By Stephen Douglas in forum New To Java
    Replies: 2
    Last Post: 04-07-2010, 08:38 PM
  3. Problem with updating JTable
    By kwaspl in forum New To Java
    Replies: 2
    Last Post: 12-20-2009, 11:41 PM
  4. inserting values from jtable into database
    By sandeepsai17 in forum New To Java
    Replies: 1
    Last Post: 06-29-2009, 07:31 PM
  5. how to populate large record in jTable
    By ahmed88 in forum New To Java
    Replies: 4
    Last Post: 04-17-2009, 07:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts