Refresh a table

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2005
Posts: 52
Reputation: Kob0724 is an unknown quantity at this point 
Solved Threads: 0
Kob0724's Avatar
Kob0724 Kob0724 is offline Offline
Junior Poster in Training

Refresh a table

 
0
  #1
Jul 8th, 2007
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:
  1. runDisplay.setModel(
  2. new javax.swing.table.DefaultTableModel(
  3. makeTableArray(),
  4. new String [] {
  5. "Date", "Distance (Miles)", "Run Time (hh:mm:ss)", "Run Time (Minutes)", "Avg. Time (Min/Mile)", "Route", "Conditions", "Notes"
  6. }
  7. ) {
  8. Class[] types = new Class [] {
  9. 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
  10. };
  11.  
  12. public Class getColumnClass(int columnIndex) {
  13. return types [columnIndex];
  14. }
  15. });

makeTablearray() method:
  1.  
  2. private Object[][] makeTableArray(){
  3. ArrayList tempLog = log1.getLog();
  4. Object table[][] = new Object[tempLog.size()][8];
  5.  
  6. //dates
  7. for(int i = 0; i < tempLog.size(); i++){
  8. table[i][0] = ((Run)tempLog.get(i)).getDate().toString();
  9. }
  10.  
  11. //distance
  12. for(int i = 0; i < tempLog.size(); i++){
  13. table[i][1] = "" + ((Run)tempLog.get(i)).getDist();
  14. }
  15.  
  16. //time HMS
  17. for(int i = 0; i < tempLog.size(); i++){
  18. table[i][2] = ((Run)tempLog.get(i)).getTime().toString();
  19. }
  20.  
  21. //time in Min
  22. for(int i = 0; i < tempLog.size(); i++){
  23. table[i][3] = "" + ((Run)tempLog.get(i)).getTime().getMinTime();
  24. }
  25.  
  26. //min per mile
  27. for(int i = 0; i < tempLog.size(); i++){
  28. table[i][4] = "" + (((Run)tempLog.get(i)).getTime().getMinTime())/(((Run)tempLog.get(i)).getDist());
  29. }
  30.  
  31. //route
  32. for(int i = 0; i < tempLog.size(); i++){
  33. table[i][5] = ((Run)tempLog.get(i)).getRoute();
  34. }
  35.  
  36. //conditions
  37. for(int i = 0; i < tempLog.size(); i++){
  38. table[i][6] = ((Run)tempLog.get(i)).getCon();
  39. }
  40.  
  41. //notes
  42. for(int i = 0; i < tempLog.size(); i++){
  43. table[i][7] = ((Run)tempLog.get(i)).getNotes();
  44. }
  45.  
  46. return table;
  47. }
Last edited by Kob0724; Jul 8th, 2007 at 5:01 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 437
Reputation: Fungus1487 is on a distinguished road 
Solved Threads: 50
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Pro in Training

Re: Refresh a table

 
0
  #2
Jul 8th, 2007
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
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 52
Reputation: Kob0724 is an unknown quantity at this point 
Solved Threads: 0
Kob0724's Avatar
Kob0724 Kob0724 is offline Offline
Junior Poster in Training

Re: Refresh a table

 
0
  #3
Jul 9th, 2007
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.
Last edited by Kob0724; Jul 9th, 2007 at 11:47 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 437
Reputation: Fungus1487 is on a distinguished road 
Solved Threads: 50
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Pro in Training

Re: Refresh a table

 
0
  #4
Jul 9th, 2007
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.
  1. runDisplay.setModel(
  2. new javax.swing.table.DefaultTableModel(
  3. makeTableArray(),
  4. new String [] {
  5. "Date", "Distance (Miles)", "Run Time (hh:mm:ss)", "Run Time (Minutes)", "Avg. Time (Min/Mile)", "Route", "Conditions", "Notes"
  6. }
  7. ) {
  8. Class[] types = new Class [] {
  9. 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
  10. };
  11.  
  12. public Class getColumnClass(int columnIndex) {
  13. return types [columnIndex];
  14. }
  15. });
all you have to do is make another method e.g.
  1. 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
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,473
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 514
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Refresh a table

 
0
  #5
Jul 9th, 2007
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.
Last edited by Ezzaral; Jul 9th, 2007 at 1:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 52
Reputation: Kob0724 is an unknown quantity at this point 
Solved Threads: 0
Kob0724's Avatar
Kob0724 Kob0724 is offline Offline
Junior Poster in Training

Re: Refresh a table

 
0
  #6
Jul 10th, 2007
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();
            resetTableModel();
        }
            
    }                       
    }

  1. private void resetTableModel(){
  2. runDisplay.setModel(new javax.swing.table.DefaultTableModel(
  3. makeTableArray(),
  4. new String [] {
  5. "Date", "Distance (Miles)", "Run Time (hh:mm:ss)", "Run Time (Minutes)", "Avg. Time (Min/Mile)", "Route", "Conditions", "Notes"
  6. }
  7. ) {
  8. Class[] types = new Class [] {
  9. 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
  10. };
  11.  
  12. public Class getColumnClass(int columnIndex) {
  13. return types [columnIndex];
  14. }
  15. });
  16. }

I'm not sure if this is exactly the most efficient way of doing things. But it works for now. Thanks for the help.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,473
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 514
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Refresh a table

 
0
  #7
Jul 10th, 2007
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.
Last edited by Ezzaral; Jul 10th, 2007 at 12:47 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 52
Reputation: Kob0724 is an unknown quantity at this point 
Solved Threads: 0
Kob0724's Avatar
Kob0724 Kob0724 is offline Offline
Junior Poster in Training

Re: Refresh a table

 
0
  #8
Jul 10th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,473
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 514
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Refresh a table

 
0
  #9
Jul 10th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC