Results 1 to 20 of 23
-
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.
- 08-27-2012, 04:16 PM #2
Senior Member
- Join Date
- Oct 2011
- Location
- Sweden
- Posts
- 123
- Rep Power
- 0
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.
- 08-27-2012, 04:26 PM #3
Re: problem in adding values in jtable column
Is this the same problem: help with jtable | DaniWeb
If you don't understand my response, don't ignore it, ask a question.
-
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
- 08-27-2012, 04:56 PM #5
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#codeIf you don't understand my response, don't ignore it, ask a question.
-
Re: problem in adding values in jtable column
this is the error i am facingJava 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)
- 08-27-2012, 05:24 PM #7
Re: problem in adding values in jtable column
It looks like the getValueAt() method returns a String object not an Integer object.java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
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.If you don't understand my response, don't ignore it, ask a question.
-
Re: problem in adding values in jtable column
can you tell me where i should this statement. i am realy confused.
- 08-27-2012, 07:44 PM #9
Re: problem in adding values in jtable column
Inside the loop where you call the getValueAt() method.
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?Java Code:for ( i=0; i<row ; i++){ System.out.println("i=" + i + " gVA=" + CARTtbl.getValueAt(i,1)); total = total+(Integer) CARTtbl.getValueAt(i,1); }Last edited by Norm; 08-27-2012 at 10:45 PM. Reason: syntax error
If you don't understand my response, don't ignore it, ask a question.
-
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 labelLast edited by blustar angel; 08-27-2012 at 09:51 PM.
- 08-27-2012, 10:12 PM #11
Re: problem in adding values in jtable column
Please post what is printed out by the println statement that you added.
If you don't understand my response, don't ignore it, ask a question.
-
Re: problem in adding values in jtable column
it prints out the value as well as error also comes.Java 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)
:(
- 08-27-2012, 10:44 PM #13
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?i=0 gVA= 178
What try of object is returned by the getValueAt() method? Change the println to this to see:
When you find out what type the object is you will know how to get a numeric value from it.Java Code:System.out.println("i=" + i + " gVA="+ CARTtbl.getValueAt(i,1).getClass()); // show classLast edited by Norm; 08-27-2012 at 10:47 PM.
If you don't understand my response, don't ignore it, ask a question.
-
Re: problem in adding values in jtable column
this is the output plus the error which i got previously.i=0 gVA=178Last edited by blustar angel; 08-27-2012 at 11:19 PM.
-
Re: problem in adding values in jtable column
are u sure wether my coding is correct.i am a newbie so it takes time.
- 08-28-2012, 01:13 AM #16
Re: problem in adding values in jtable column
How can that be with a program with over 6691 lines????i am a newbie so it takes time.
What was printed when you Changed the println to this:
Java Code:System.out.println("i=" + i + " gVA="+ CARTtbl.getValueAt(i,1).getClass()); // show classIf you don't understand my response, don't ignore it, ask a question.
-
Re: problem in adding values in jtable column
[QUOTE][i=0 gVA=class java.lang.String/QUOTE]
is the output.
- 08-28-2012, 01:28 PM #18
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.
If you don't understand my response, don't ignore it, ask a question.
-
Re: problem in adding values in jtable column
can u tell me the syntax .because i dont no how to write it.
- 08-28-2012, 07:03 PM #20
Similar Threads
-
Calculating values from a jtable column
By victaino in forum New To JavaReplies: 37Last Post: 05-24-2012, 11:16 AM -
adding a listener to a jtable column header
By fam2315 in forum AWT / SwingReplies: 0Last Post: 10-07-2011, 05:16 PM -
Displaying database values as a row in a JTable but not as a column
By pratim in forum AWT / SwingReplies: 1Last Post: 03-24-2011, 12:51 PM -
adding sum column on jtable that is binded to a database table
By madcloud in forum Enterprise JavaBeans (EJB)Replies: 4Last Post: 12-06-2010, 08:26 AM -
Adding JComboBox type column to the JTable ??
By Stephen Douglas in forum New To JavaReplies: 2Last Post: 04-10-2010, 03:03 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks