Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: How to get the sum value from a jtable column

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Post How to get the sum value from a jtable column

    login page.jpg

    I want this password field (textfield) validated.. as showing *** or . Instead of showing 12345

    &


    jtablemenu.jpg


    Here in the table I want to get the total amount from Amount column

    When I click get total button the total amount (520.00) has to come in this textfld.
    It has to be taken from the amount columns


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,038
    Thanks
    52
    Thanked 2,280 Times in 2,251 Posts

    Default Re: How to get the sum value from a jtable column

    Please post the code you have questions about and indicate where you are having problems.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2009
    Posts
    2
    My Mood
    Where
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to get the sum value from a jtable column

    For the Password Field, look at this: JPasswordField

    As for the calculating the Total from the Table, like Norm said, what have you tried?

  4. The Following User Says Thank You to StormyWaters For This Useful Post:

    Asri (February 7th, 2013)

  5. #4
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to get the sum value from a jtable column

    i have a jtable which loads data from a database about items' details and the last column of the table is amount column.. it shows the price of item multiplied by item quantity.
    and if i add many items and i want to get the totaol(sum) of all amounts to a textfeild..

    here is my code i paste it from IDE
    ____________________________________________

    Connection con = DB.itzMyConnection();
    java.sql.Statement S = con.createStatement();

    int Itmcd = Integer.parseInt( jTextField8.getText());


    ResultSet r = S.executeQuery("Select * from items where ItemCode= '" jTextField8.getText() "'");
    while (r.next()) {

    String s1 = r.getString("ItemCode");
    double unitPrice = Double.parseDouble(r.getString("Price"));
    int Qty = Integer.parseInt(jTextField7.getText());

    double amnt = unitPrice * Qty;

    DefaultTableModel dft = (DefaultTableModel) jTable1.getModel();
    Vector v1 = new Vector();
    v1.add(s1);
    v1.add(r.getString("ItemCategory"));
    v1.add(r.getString("ItemName"));
    v1.add(jTextField9.getText());
    v1.add(Qty);
    v1.add(unitPrice);
    v1.add(amnt);

    dft.addRow(v1);
    }

    }
    here the amount columns total sum should be extracted out. help me pls

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,038
    Thanks
    52
    Thanked 2,280 Times in 2,251 Posts

    Default Re: How to get the sum value from a jtable column

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.


    Where is the data for the "amount columns"?
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    Asri (February 8th, 2013)

  8. #6
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to get the sum value from a jtable column

    i have tried with using methods like getColumnCount(); getRowCount(); and getValueIn() with a for loop, but I had to face a hard time to get done with those coding. so i am not capable of providing a code right now. so if it is possible. can any one direct me through some possible methods and codings.

    < DefaultTableModel dft = (DefaultTableModel) jTable1.getModel();
     
     
             int row = dft.getRowCount();
             System.out.println(row);
            int columnCount = dft.getColumnCount();
                System.out.println(columnCount);
     
     
     
             int valueAt = (Integer)jTable1.getValueAt(1, 5);
            System.out.println(valueAt);
     
                  for(int i=0; i <= row; i++){
                  int i2  =(Integer)dft.getValueAt(i, 6);
                  System.out.println(i2);
                  >

    thank you.
    And @Norm how can i add u as a frnd

  9. #7
    Junior Member
    Join Date
    Feb 2013
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: How to get the sum value from a jtable column

    finally got the solution
      private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
     
     
     
            JOptionPane.showMessageDialog(this, "eeeee");
     
            if(evt.getSource().equals(jButton1))
     
            {
     
                JOptionPane.showMessageDialog(this, "tr tyt");
     
     
     
            double total=0;
     
           int  rows = jTable1.getRowCount();
     
            for (int i = 0;  i < rows; i++) 
     
            { 
     
                total += ( Double.parseDouble(jTable1.getValueAt(i, 1).toString())) ; 
     
     
     
            }
     
     
     
            JOptionPane.showMessageDialog(this,total);
     
     
     
            }
     
     
     
        }

Similar Threads

  1. JTable Column Sizing
    By Jeff.Steinkamp in forum AWT / Java Swing
    Replies: 1
    Last Post: January 20th, 2013, 08:23 PM
  2. sum of each row & column in 2-dimensional array.
    By muhammad waqar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 5th, 2013, 07:09 AM
  3. JTable column widths reverting to defaults?
    By eewill in forum AWT / Java Swing
    Replies: 3
    Last Post: January 25th, 2012, 02:49 PM
  4. Replies: 3
    Last Post: October 26th, 2011, 03:37 PM
  5. Highlighting both row and column in JTable
    By bschneider14 in forum AWT / Java Swing
    Replies: 4
    Last Post: May 29th, 2010, 09:14 AM