1.11M Members

help with jtable

 
0
 

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

 
0
 

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.

Question Answered as of 1 Year Ago by JamesCherrill
 
0
 

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

 
0
 

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

 
0
 

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

 
0
 

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

 
0
 

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

 
0
 

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.

 
0
 

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?

 
0
 
 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

 
0
 

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

 
0
 

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

 
0
 

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.

 
0
 
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

 
0
 

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.

 
0
 

nothing prints out.
:(

 
0
 

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

 
0
 

no the code does not have errors.

 
0
 

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.

 
0
 
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.
:(

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article