problem in adding values in jtable column
hi,
i need solution urgently for this problem.
i have a table cartTBL that has 2 columns name and price,i need to add all the values in the price column and display it in a label .the amount should be changed as an item is added or removed.
i tried many codes ,nothing seems to work.
Re: problem in adding values in jtable column
First of all, post some of your code. We need to know more about what you've tried (and not) before we can help you.
Secondly, we need to know what your code looks like so we can tell you right away if you are doing it wrong or not.
When you say "display it in a label", what do you mean? Do you want to display it in a JTable cell OR a JLabel somewhere else?
Are you using a table model?
If you post code, use the [CODE]-tags.
Re: problem in adding values in jtable column
Is this the same problem: help with jtable | DaniWeb
Re: problem in adding values in jtable column
DefaultTableModel tm = (DefaultTableModel)cartTBL.getModel();
int total=0;
int i;
int row=CARTtbl.getRowCount();
for ( i=0; i<row ; i++){
total = total+(Integer) CARTtbl.getValueAt(i,1);
}
LBL.setText( ""+subtotal);
this is the coding
Re: problem in adding values in jtable column
Can you explain what the problem is? What does the code do or not do? Are there any error messages? If so, please post them.
Please wrap posted code in code tags:
http://www.java-forums.org/misc.php?do=bbcode#code
Re: problem in adding values in jtable column
Code:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at HOMEPAGE.totalActionPerformed(HOMEPAGE.java:6691)
at HOMEPAGE.access$2100(HOMEPAGE.java:15)
at HOMEPAGE$23.actionPerformed(HOMEPAGE.java:625)
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)
this is the error i am facing
Re: problem in adding values in jtable column
Quote:
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
It looks like the getValueAt() method returns a String object not an Integer object.
To see what is returned, add a println() statement that prints out what is returned by getValueAt().
If the String is all digits you could use the Integer class's parseInt() method to convert the String to an int.
Re: problem in adding values in jtable column
can you tell me where i should this statement. i am realy confused.
Re: problem in adding values in jtable column
Inside the loop where you call the getValueAt() method.
Code:
for ( i=0; i<row ; i++){
System.out.println("i=" + i + " gVA=" + CARTtbl.getValueAt(i,1));
total = total+(Integer) CARTtbl.getValueAt(i,1);
}
The error message you posted shows that the program you are working on has over 6691 lines. Its hard to imagine how you got such a large program and don't know how to debug problems. Can you explain how you got this code?
Re: problem in adding values in jtable column
i am doing a project in my school.
even i am amazed that i have so much of line.
it returns an int value.now what should i do.how can i display it in a label
Re: problem in adding values in jtable column
Please post what is printed out by the println statement that you added.
Re: problem in adding values in jtable column
Code:
i=0 gVA= 178
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at HOMEPAGE.totalActionPerformed(HOMEPAGE.java:6692)
at HOMEPAGE.access$2100(HOMEPAGE.java:15)
at HOMEPAGE$23.actionPerformed(HOMEPAGE.java:625)
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)
it prints out the value as well as error also comes.
:(
Re: problem in adding values in jtable column
Is that space between the = and the 178 in the object or was it in the println?
What try of object is returned by the getValueAt() method? Change the println to this to see:
Code:
System.out.println("i=" + i + " gVA="+ CARTtbl.getValueAt(i,1).getClass()); // show class
When you find out what type the object is you will know how to get a numeric value from it.
Re: problem in adding values in jtable column
this is the output plus the error which i got previously.
Re: problem in adding values in jtable column
are u sure wether my coding is correct.i am a newbie so it takes time.
Re: problem in adding values in jtable column
Quote:
i am a newbie so it takes time.
How can that be with a program with over 6691 lines????
What was printed when you Changed the println to this:
Code:
System.out.println("i=" + i + " gVA="+ CARTtbl.getValueAt(i,1).getClass()); // show class
Re: problem in adding values in jtable column
[QUOTE][i=0 gVA=class java.lang.String/QUOTE]
is the output.
Re: problem in adding values in jtable column
If the object is a String that contains numeric digits, you can use the Integer class's parse method to convert it to an int that can be added to the total.
Re: problem in adding values in jtable column
can u tell me the syntax .because i dont no how to write it.
Re: problem in adding values in jtable column
Have you tried Searching for code samples here on the forum or with Google?
Have you read the API doc for the Integer class?