Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    blustar angel is offline Member
    Join Date
    Aug 2012
    Posts
    17
    Blog Entries
    1
    Rep Power
    0

    Default 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.

  2. #2
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default 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.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default 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.

  4. #4
    blustar angel is offline Member
    Join Date
    Aug 2012
    Posts
    17
    Blog Entries
    1
    Rep Power
    0

    Default 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

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default 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
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    blustar angel is offline Member
    Join Date
    Aug 2012
    Posts
    17
    Blog Entries
    1
    Rep Power
    0

    Default Re: problem in adding values in jtable column

    Java 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

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: problem in adding values in jtable column

    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.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    blustar angel is offline Member
    Join Date
    Aug 2012
    Posts
    17
    Blog Entries
    1
    Rep Power
    0

    Default Re: problem in adding values in jtable column

    can you tell me where i should this statement. i am realy confused.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: problem in adding values in jtable column

    Inside the loop where you call the getValueAt() method.
    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);
         }
    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?
    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.

  10. #10
    blustar angel is offline Member
    Join Date
    Aug 2012
    Posts
    17
    Blog Entries
    1
    Rep Power
    0

    Default 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
    Last edited by blustar angel; 08-27-2012 at 09:51 PM.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default 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.

  12. #12
    blustar angel is offline Member
    Join Date
    Aug 2012
    Posts
    17
    Blog Entries
    1
    Rep Power
    0

    Default Re: problem in adding values in jtable column

    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)
    it prints out the value as well as error also comes.
    :(

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: problem in adding values in jtable column

    i=0 gVA= 178
    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:
    Java 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.
    Last edited by Norm; 08-27-2012 at 10:47 PM.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    blustar angel is offline Member
    Join Date
    Aug 2012
    Posts
    17
    Blog Entries
    1
    Rep Power
    0

    Default Re: problem in adding values in jtable column

    i=0 gVA=178
    this is the output plus the error which i got previously.
    Last edited by blustar angel; 08-27-2012 at 11:19 PM.

  15. #15
    blustar angel is offline Member
    Join Date
    Aug 2012
    Posts
    17
    Blog Entries
    1
    Rep Power
    0

    Default Re: problem in adding values in jtable column

    are u sure wether my coding is correct.i am a newbie so it takes time.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default Re: problem in adding values in jtable column

    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:
    Java Code:
     System.out.println("i=" + i + " gVA="+  CARTtbl.getValueAt(i,1).getClass());  // show class
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    blustar angel is offline Member
    Join Date
    Aug 2012
    Posts
    17
    Blog Entries
    1
    Rep Power
    0

    Default Re: problem in adding values in jtable column

    [QUOTE][i=0 gVA=class java.lang.String/QUOTE]
    is the output.

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default 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.

  19. #19
    blustar angel is offline Member
    Join Date
    Aug 2012
    Posts
    17
    Blog Entries
    1
    Rep Power
    0

    Default Re: problem in adding values in jtable column

    can u tell me the syntax .because i dont no how to write it.

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,562
    Rep Power
    25

    Default 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?
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Calculating values from a jtable column
    By victaino in forum New To Java
    Replies: 37
    Last Post: 05-24-2012, 11:16 AM
  2. adding a listener to a jtable column header
    By fam2315 in forum AWT / Swing
    Replies: 0
    Last Post: 10-07-2011, 05:16 PM
  3. Replies: 1
    Last Post: 03-24-2011, 12:51 PM
  4. adding sum column on jtable that is binded to a database table
    By madcloud in forum Enterprise JavaBeans (EJB)
    Replies: 4
    Last Post: 12-06-2010, 08:26 AM
  5. Adding JComboBox type column to the JTable ??
    By Stephen Douglas in forum New To Java
    Replies: 2
    Last Post: 04-10-2010, 03:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •