I'm working on a chemical calculator and using a JTable to do this. There are 4 rows in total: name, quantity, grams and mls.
I have a form that allows the user to select the name of a chemical and a quantity and pressing a button adds this to the first two columns of each row.
The other two columns are calculated values that depend on the quantities of all the chemicals, as well as other information held in a database about each chemical.
I would like these two columns to be populated when I click my 'calculate' button.
I'm not really sure how to go about this as I've already used the getValueAt() method of the table model to populate just the first two columns of each row. Can this even be done? Or does the table have to be populated as a whole in one action?
Would it be better to create another table with all the values after the grams and mls have been calculated and have this new table displayed in place of the first?
Sorry if I'm not explaining this very well, I'm pretty new to Swing. If you need any clarifications, just let me know.
Thanks for the response, I'll be sure to check that out.
I have one more question. When you add a row to a JTable and then call fireDataTableChanged(), is the entire table repopulated or is it just the new row that is added? That is, are the methods to add a row to the table called again for each row or just for the row being added?
I know precious little about JTables, but have you looked at the documentation for that method to add rows? What does it say? The fireXXX() methods usually don't change the source but send an event to some object listening for it.
Thanks for the reply, Campbell. Ok, so it seems that is does redraw the entire table.
So I'm thinking maybe I could set up a sort of flag so that if the flag is set to false, then the calculated values are simply returned as null (since I need the total quantities to calculated them), and if it is set to true (on pressing the 'calculate button') then a method is called to return the proper calculated values. Hmm I'll need to think on that but I could end up running into scope problems if I'm changing variables in other classes...
So what I tried was adding a private static boolean 'state' (to be renamed) variable to the row creator class and a static method that can change its value. When the 'calculate' button is pressed, my controller class calls that method to change the value and the table is redrawn.
When state is false the getter for grams return null and when it is true, the getter will return the calculated value.
I've managed to get it to work doing this but if this sounds like absolute crap, please say.
Château Na Fleur wrote:I have one more question. When you add a row to a JTable and then call fireDataTableChanged(), is the entire table repopulated or is it just the new row that is added? That is, are the methods to add a row to the table called again for each row or just for the row being added?
For the first of your questions: Neither.
And for the second question: The fireDataTableChanged() method doesn't modify the JTable in any way. It just notifies the objects that were listening to the table model (Note: You don't add rows to a JTable, you add the rows to a table model. The JTable will be one of those listeners and will react by redrawing itself.)
And my question: Why aren't you calling fireTableRowsInserted(row, row) when you insert a row into a table model?
Been there. Done that. Went back for more. But this time, I took this tiny ad with me:
SKIP - a book about connecting industrious people with elderly land owners