hey everyone,
i am trying to write a code that will calculate the sum of all values in a column of Jtable.
the total sum will change when a row is removed or added.
i tried many ways.
i need help urjently.
:)thanks

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.

can you please give me the coding of the loop.
i just staretd learning java this year.
:)
thanks

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

here is the coding i have written

int subtotal=0;
      int i;
      int row= CARTtbl.getRowCount();

      for(i=0 ; i<row ; i++){
            Object val=CARTtbl.getValueAt(i, 1);
      subTOT.setText( subtotal+val);
      }

i am getting error at line 6. its a project i am doing,an online shopping site

Please always quote the exact complete error message when you get an error.

bad operend types for binary operators '+' first type: int
second type:java.lang.object
this is the error message it shows.

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.

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?

 int subtotal=0;
     int row=CARTtbl.getRowCount();
     for(int i=0; i< row; i++){
      subtotal =  subtotal+ CARTtbl.getValueAt(i,1);
     }
     subTOT.setText(subtotal); 

this is the code and here is the error i am facing

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - bad operand types for binary operator '+'
  first type:  int
  second type: java.lang.Object
    at HOMEPAGE.totalActionPerformed(HOMEPAGE.java:6623)
    at HOMEPAGE.access$2100(HOMEPAGE.java:15)
    at HOMEPAGE$23.actionPerformed(HOMEPAGE.java:629)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:703)
    at java.awt.EventQueue.access$000(EventQueue.java:102)
    at java.awt.EventQueue$3.run(EventQueue.java:662)
    at java.awt.EventQueue$3.run(EventQueue.java:660)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:676)
    at java.awt.EventQueue$4.run(EventQueue.java:674)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:673)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

i realy need help

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

thanks a lot
but i am not sure how to convert it into integer or double.
i am able to convert it to string only

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.

int subtotal=0;
     int row=CARTtbl.getRowCount();
     for (int i=0; i<row ; i++){
     System.out.println(CARTtbl.getValueAt(i,1));
     }

is this how i should do it
i am realy confused

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.

nothing prints out.
:(

Strange. Does the code compile without errors? Is the println statement being executed?

no the code does not have errors.

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.

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: Uncompilable source code - bad operand types for binary operator '+'
  first type:  int
  second type: java.lang.Object
    at HOMEPAGE.totalActionPerformed(HOMEPAGE.java:6705)
    at HOMEPAGE.access$2100(HOMEPAGE.java:17)
    at HOMEPAGE$23.actionPerformed(HOMEPAGE.java:631)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:703)
    at java.awt.EventQueue.access$000(EventQueue.java:102)
    at java.awt.EventQueue$3.run(EventQueue.java:662)
    at java.awt.EventQueue$3.run(EventQueue.java:660)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:676)
    at java.awt.EventQueue$4.run(EventQueue.java:674)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:673)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

yes i am getting the same error.
:(

is there a code through which i can display the sum of values in the column directly in a label without a button and it changes when a row is removed or added.

@kraykezia
You started with this question. You wrote some code that has an error. People tried to help you with that error, but you still have not fixed it. Now you ask the same question again. This is getting you (and us) nowhere.

What prints out from the println you added just before the statement where the error happens?
What you posted last did not show the output from that println.

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.