So I've got this jtable set up that displays the contents of an arraylist by use of the handy dandy table model (see code below). you'll notice it calls a method makeTableArray() (see code below) in order to figure out what the array that its going to use is. In another part of the program I enter data into this form I made, and it inserts another object in the to the arraylist I was using in the table model. I was wondering, how do I get the table to "refresh" after I submit this new information into the arraylist. I submit the information via a button. Any help is greatly appreciated. I'm using NetBeans IDE 5.5

Table model:

runDisplay.setModel(
new javax.swing.table.DefaultTableModel(
            makeTableArray(),
            new String [] {
                "Date", "Distance (Miles)", "Run Time (hh:mm:ss)", "Run Time (Minutes)", "Avg. Time (Min/Mile)", "Route", "Conditions", "Notes"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });

makeTablearray() method:

private Object[][] makeTableArray(){
        ArrayList tempLog = log1.getLog();
        Object table[][] = new Object[tempLog.size()][8];
        
        //dates
        for(int i = 0; i < tempLog.size(); i++){
            table[i][0] = ((Run)tempLog.get(i)).getDate().toString();
        }
        
        //distance
        for(int i = 0; i < tempLog.size(); i++){
            table[i][1] = "" + ((Run)tempLog.get(i)).getDist();
        }
        
        //time HMS
         for(int i = 0; i < tempLog.size(); i++){
            table[i][2] = ((Run)tempLog.get(i)).getTime().toString();
        }
        
        //time in Min
         for(int i = 0; i < tempLog.size(); i++){
            table[i][3] = "" + ((Run)tempLog.get(i)).getTime().getMinTime();
         }
            
        //min per mile
         for(int i = 0; i < tempLog.size(); i++){
            table[i][4] = "" + (((Run)tempLog.get(i)).getTime().getMinTime())/(((Run)tempLog.get(i)).getDist());
        }
        
        //route
        for(int i = 0; i < tempLog.size(); i++){
            table[i][5] = ((Run)tempLog.get(i)).getRoute();
        }
        
        //conditions
         for(int i = 0; i < tempLog.size(); i++){
            table[i][6] = ((Run)tempLog.get(i)).getCon();
         }
         
        //notes
         for(int i = 0; i < tempLog.size(); i++){
            table[i][7] = ((Run)tempLog.get(i)).getNotes();
         }
        
        return table;
    }

im pretty certain you will have to recall the 'table.setmodel' again. As it is not dynamically updating from the array it just sets the table to the data given at the tume the table is created. probably best to create another method that creates your table and sets its defaults etc then call this when you need to update. hope that helps

I'm not sure I understand. How would this work? I'm using NetBeans so a lot of the swing code has been pregenerated for me.

just like you have your make table array method in a seperate method which is then called to get the current array you can do the same with creating the table

in your code you will have the INIT() statement and inside this method will be your table being created i.e.

runDisplay.setModel(
new javax.swing.table.DefaultTableModel(
            makeTableArray(),
            new String [] {
                "Date", "Distance (Miles)", "Run Time (hh:mm:ss)", "Run Time (Minutes)", "Avg. Time (Min/Mile)", "Route", "Conditions", "Notes"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });

all you have to do is make another method e.g.

private void CreateMyTable(){ .... table code above ....}

and remove it from the INIT method and replace it with CreateMyTable()

therefore it will still create your table but whenever you need to refresh it you can call 'CreateMyTable()' ? understand? if not let me know which bits u dont quite understand. Hope it helps

Since you are using DefaultTableModel, take a look at the addRow() and insertRow() methods. They both take the row as a Vector, not an Object[] array row, but you could work around that easily.

I got it. I just told the table to set a new model every time I add a new item on to the table (a.k.a. the enter button is clicked).

private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
        
        
        Date tempDate = checkDate();
        Time tempTime = checkTime();
        double dist = checkDist();
        String route = routeArea.getText();
        String conditions = conArea.getText();
        String notes = notesArea.getText();
        
        if(tempDate != null && tempTime != null && dist != -1.0){
            log1.insertRun(tempTime, dist, tempDate, route, conditions, notes);
            resultLabel2.setText("Run inserted");
            conArea.setText("");
            routeArea.setText("");
            notesArea.setText("");
            timeHours.setText("00");
            timeMinutes.setText("00");
            timeSeconds.setText("00");
            distance.setText("");
            setDefaultDate();
            [B]resetTableModel();[/B]
        }
            
    }                       
    }
private void resetTableModel(){
         runDisplay.setModel(new javax.swing.table.DefaultTableModel(
            makeTableArray(),
            new String [] {
                "Date", "Distance (Miles)", "Run Time (hh:mm:ss)", "Run Time (Minutes)", "Avg. Time (Min/Mile)", "Route", "Conditions", "Notes"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
    }

I'm not sure if this is exactly the most efficient way of doing things. But it works for now. Thanks for the help.

Well, you are right that it isn't the most efficient way, but if your table is small and the performance is fine then stick with what works for you :) You can always come back to it if the performance becomes an issue.

Yea. The table is just a bunch of really short string values from an arraylist. It's not like the content of the cells is anything all that complex. The other reason I'm keeping it like this for now is that the data displayed in the table comes from a sorted arraylist. So when anything gets added to the table/arraylist it's not just the simple matter of adding a new row at the top or bottom. Depending on the data entered, it might need to be inserted in the dead center of the table.

Ah, yes a sorted model would require more work on your part. Certainly doable by sorting the row data vector that backs the table model or by implementing the table model interface directly against your data source, but probably more work than it would be worth at this point :)

Glad you got it working to your expectations.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.