| | |
Refresh a table
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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:
makeTablearray() method:
Table model:
Java Syntax (Toggle Plain Text)
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:
Java Syntax (Toggle Plain Text)
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; }
Last edited by Kob0724; Jul 8th, 2007 at 5:01 pm.
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
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
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. all you have to do is make another method e.g. 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
in your code you will have the INIT() statement and inside this method will be your table being created i.e.
Java Syntax (Toggle Plain Text)
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]; } });
Java Syntax (Toggle Plain Text)
private void CreateMyTable(){ .... table code above ....}
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
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
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).
I'm not sure if this is exactly the most efficient way of doing things. But it works for now. Thanks for the help.
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();
}
}
} Java Syntax (Toggle Plain Text)
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.
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.

Glad you got it working to your expectations.
![]() |
Similar Threads
- Javascript and Firefox ISSUES (JavaScript / DHTML / AJAX)
Other Threads in the Java Forum
- Previous Thread: How to add a jar within another jar?
- Next Thread: Question about Applet (Resolution vs FPS) + other
| Thread Tools | Search this Thread |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class client code compile compiler component database developmenthelp eclipse error fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html ide image input integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loop loops mac main map method methods mobile myregfun netbeans newbie notdisplaying number online page print problem program programming project qt recursion scanner screen server set singleton size sms sort spamblocker sql string swing system template textfields threads time title tree tutorial-sample update variablebinding windows working xor






