You can get the contents of any cell in the table, so you just need a loop that accesses every row in one column. When the data is changed you can add a listener to the table that will be called so you can recalculate the sum.
Study the API documenation foir JTable and you will find the info you need. (Plus, you will develop essential skill in using the API doc.)
Then try to put what you have learned into code. If/when you get into difficulties you can post your code here and someone will help.
JamesCherrill
... trying to help
8,675 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,479
Skill Endorsements: 33
Question Answered as of 9 Months Ago by
JamesCherrill There are lots of people here who will freely give their time to help you become the best Java programmer you can be. There's nobody here who is interested in helping you cheat or doing your homework for you.
DaniWeb Member Rules include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules
JamesCherrill
... trying to help
8,675 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,479
Skill Endorsements: 33
Please always quote the exact complete error message when you get an error.
JamesCherrill
... trying to help
8,675 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,479
Skill Endorsements: 33
That is NOT the error message, it's just your version of it.
Always quote the exact complete error message when you get an error.
Not your own edited summary.
Copy/paste the whole text from the compiler.
Anyway, in this case it's obvious. You are trying to add an integer (OK) to an Object (not OK). You don't say what kind of Objects you have put in your table, so I'm going to guess that they are Integers. When you call getValueAt it returns an Object, so you have to cast it to Integer so the compiler knows what it is, and that it can be used in an arithmetic formula.
JamesCherrill
... trying to help
8,675 posts since Apr 2008
Reputation Points: 2,636
Solved Threads: 1,479
Skill Endorsements: 33
The '+' operator is used for adding two numbers together or for concatenating two Strings.
The compiler does NOT know what to do when one of the operands is type Object and gives an error message.
What datatype is val? Can it be cast to a String or a number?
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
You can not use the + operator with an object of type Object.
One of the few objects you can use the + operator with is the String class.
You need to convert the item in CARTtbl to a numeric type: (int, double, etc) if you want to do arithmetic with it. Another way would be to cast it to one of the numeric wrapper classes like Integer or Double and let the compiler unbox it
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
What datatype is the object returned by getValueAt()? You need to know that to be able to get a numeric value that can be added to the total. Add a println() to print out the object so you can see what it is.
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
What prints out when you execute that code?
What type of data is returned by the getValueAt() method? The println should show you.
Knowing what type it is will help you get a numeric value from it.
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
Strange. Does the code compile without errors? Is the println statement being executed?
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16
Restore this line right after the println you added:
subtotal = subtotal+ CARTtbl.getValueAt(i,1);
and see if you get the same error that you did before. The println output should go to the same place the error message prints.
NormR1
Posting Sage
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16