sum of all values in a column of jtable

Ads
 

sum of all values in a column of jtable

hey everyone, is there a code to display the sum of all values in a column of a jtable namely CARTtbl in a jabel without using a button,and the sum will change when an item is added or deleted.

View Answers

August 27, 2012 at 4:11 PM

Here is an example of jtable that sums up the values of two columns and display the result in another column.

 import javax.swing.*;
 import javax.swing.table.*;
 import java.awt.*;
 import java.awt.event.*;

public class JTableAction {
  JTable table;
  public static void main(String[] args) {
  new JTableAction();
  }

  public JTableAction(){
  JFrame frame = new JFrame("Setting Cell Values in JTable");
  JPanel panel = new JPanel();
  Integer data[][] = {{65,87,},
   {78,98,},
   {63,90,},
   {99,80,}};
  String col[] = {"Marks1","Marks","Total"};  
  DefaultTableModel model = new DefaultTableModel(data, col);
  table = new JTable(model);

  JTableHeader header = table.getTableHeader();
  header.setBackground(Color.yellow);
  JScrollPane pane = new JScrollPane(table);
  panel.add(pane);
  frame.add(panel);
  frame.setSize(500,150);
  frame.setUndecorated(true);
  frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      int rows= table.getRowCount();
      for(int i=0;i<rows;i++){
      int obj1 = Integer.parseInt(GetData(table, i, 0).toString());
      int obj2 = Integer.parseInt(GetData(table, i, 1).toString());
      int obj3=obj1+obj2;
      SetData(new Integer(obj3),i,2);
      }
      System.out.println("Total Marks is added.");
  }

  public void SetData(Object obj, int row_index, int col_index){
  table.getModel().setValueAt(obj,row_index,col_index);
  }
   public Object GetData(JTable table, int row_index, int col_index){
  return table.getModel().getValueAt(row_index, col_index);
  }
}

Ads









Related Tutorials/Questions & Answers:
sum of all values in a column of jtable
sum of all values in a column of jtable  hey everyone, is there a code to display the sum of all values in a column of a jtable namely CARTtbl... or deleted.   Here is an example of jtable that sums up the values of two
How to insert and update all column values of database from jtable.
How to insert and update all column values of database from jtable.  ... is shown in the jtable.. of my jframe window.Now as per my requirement i have to add ,update,delete database values from jtable only so i added three buttons add
Advertisements
How to insert and update all column values of database from jtable.
How to insert and update all column values of database from jtable.  ... ,update,delete database values from jtable only so i added three buttons add,update... = new String[allRows.size()]; // Get column names and values
How to insert and update all column values of database from jtable.
How to insert and update all column values of database from jtable.  ... ,update,delete database values from jtable only so i added three buttons add,update... = new String[allRows.size()]; // Get column names and values
sum in JTable
sum in JTable  how to calculate sum from JTable's one field like total
inserting all the values in a html table column
inserting all the values in a html table column  strong text hai everyone, i'm a fresher to servlet i need to know that, how can i insert all the values in a html table column into a database. Thanks in advance
JTable duplicate values in row
JTable duplicate values in row  JTable duplicate values in row
sum database column
sum database column  please i have two columns-col1 and col2, col1... 3 Soap 4 how will i query the database to sum the Items... for you. select item, sum(quqantity) from `order` group by item
write a program which asks the user for two int values and calculates their sum.
write a program which asks the user for two int values and calculates their sum.  The title says it all, an example is (1,2) > 3
How to read and retrieve jtable row values into jtextfield on clicking at particular row ...
How to read and retrieve jtable row values into jtextfield on clicking... application in which i have to display database records in jtable .now I want to read all the values of particular row at which mouse is clicked. and display
JTable
JTable  Hi I have problems in setting values to a cell in Jtable... method tableChanged(TableModelEvent e) . I'll be loading values from data base when i enter id in a column. and i want to load other table columns the values
jtable
jtable  hi Sir i am working netbeans IDE,I have a jtable when i insert values in jtable then i am unable to print all inserted values,For eg if i insert 1,2,3,4,5,6,7,8 values then , i am getting output
JTable
JTable  Values to be displayed in JTextfield when Clicked on JTable Cells
JTable
JTable  i want to delete record from JTable using a MenuItem DELETE. and values of JTable are fetched from database....please reply soon
How to update,Delete database values from jtable cells ..
How to update,Delete database values from jtable cells ..  Hello Sir, I am working on a project in which i have to fetch the values from database to jtable .Now as per my requirement i need to update and delete
How to update,Delete database values from jtable cells ..
How to update,Delete database values from jtable cells ..  Hello Sir, I am working on a project in which i have to fetch the values from database to jtable .Now as per my requirement i need to update and delete the database
How to update,Delete database values from jtable cells ..
How to update,Delete database values from jtable cells ..  hello Sir... from database into jtable of a jpanel.. Now Sir, According to my need i have to update the cell values from there only means that whatever values i ma entering
how to print all possible combination that sum to zero??
how to print all possible combination that sum to zero??  ?.... Print all possible combinations that sum to zero Example Enter a num : 7 1... each of the digits so that the resultant sum is zero. Print all posible
jtable
jtable  how to get the values from database into jtable and also add a checkbox into it and then when selected the checkbox it should again insert into database the selected chewckbox.plzz help
SQL All Column Names
SQL All Column Names       SQL All Column Names is useful when you want to see the Field, Type, Null... on 'SQL All Column Names'. To understand and grasp the example we create a table
display all the values on next page
display all the values on next page  i am inserting values... values are inserting in the database and i am moving on another page after... when i return on first page it should display my selected values in textbox
How to update table column from the values of Arraylist in java
How to update table column from the values of Arraylist in java  Hii Sir, I have an arraylist containing these values [2, 1, 1, 1, 1, 1, 1... As per my need i have to add these values into different fields of single
sum
sum  a program to find the sum of the alternative diagit of it ex- no=123456 sum=1+3+5=9
JTable
JTable   how to select a definite cell which containing a similar text containg to the one which the user entering from a jtable at runtime in java
sum
; sum=sum+s; if(i==n){ System.out.print(s...+"+"); } System.out.print("\nSum of the series : "+sum
how to validate all form field values at once.
how to validate all form field values at once.  how to validate all form field values at once. condition:no form field shold contain sometext("--select
JTable
JTable  Hello, i cannot display data from my table in the database to the cells of my JTable. please help me
how to get values for same column name from two different tables in SQL
how to get values for same column name from two different tables in SQL  how to get values for same column name from two different tables in SQL???? column name is emp_id loacated in these two tables company,employee
jtable
jtable  hey i have build a form and i m also able to add data from database to jtable along with checkbox.the only problem is that if i select multiple checkboxes the data doesnt get inserted into new database and if only one
jtable
jtable  i have build an application and i retrieve data from the database and store it in jtable.now i have to make a checkbox column in each row and also a select all option in header but i am not able to do so.i am getting
JTable
"}; JTable table=new JTable(data,labels); JScrollPane pane=new JScrollPane

Ads