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 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);
  }
}









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
Python sum dictionary values by key
Python sum dictionary values by key  Hi, In python we can easily... want to merge these dictionaries into one and sum the dictionary values by key. What is the code for Python sum dictionary values by key? Thanks
JTable values are not gettiing properly
JTable values are not gettiing properly  Sir, I created one JTable with two columns and one row i have given two values,but i getting only one... details JTable table=null; JScrollPane js=null; Container cp=null; JPanel p=null
Moving a Column in JTable
Moving a Column in JTable       This section describes, how to move a column in JTable... the first column of  JTable. The  moveColumn method  takes the index
Setting the Column Header in JTable
will help you in setting the column headers in JTable. First of all, program creates... Setting the Column Header in JTable   ... the creation of JTable without column headers. In this example you will learn
Inserting a Column in JTable
Inserting a Column in JTable     ... to insert a column in JTable at a specified location. As, you have learnt in previous... in JTable. So, in this case you must add a column always at the append position
Getting Cell Values in a JTable
value  at a specified row and column position in JTable. The cell values... Getting Cell Values in a JTable     ... values in a JTable component. It is a same as setting the cell values in a JTable
Setting Cell Values in JTable
Setting Cell Values in JTable     ... values in JTable component. For this you must have the some previous knowledge about JTable. A cell is known as the format of  a row and a column in 
Removing a Column from a JTable
Removing a Column from a JTable     ... from a JTable component that uses the table model. Removing a column from a JTable... This program helps you in how to remove a column from a JTable. For this, have
Changing the Name of Column in a JTable
Changing the Name of Column in a JTable   ... the name of column in JTable component. You have learnt the JTable containing ... the name in the name of column in a JTable in this following  example
Appending a Column in JTable
Appending a Column in JTable     ... in JTable. For adding a new column in JTable component you have to need a table model... column in JTable. The following program creates a table that contains some data
Sum of Column in a Database Table
Sum of Column in a Database Table   ... how you can get the sum of specific column. Here we are providing you an example... and name of that column of which we want to calculate the sum. If the statement gets
Enabling Row, Column and Cell Selections in a JTable
Enabling Row, Column and Cell Selections in a JTable... to describe how to enable the row, column and cell selections in a JTable... will see the enabling row, column and cell selections in a JTable. When you
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
Setting an Icon with Text in a Column Head of JTable
Setting an Icon with Text in a Column Head of JTable... to set an icon with text in a column head of JTable component. But what... a JTable containing some data and column with column header. After that you
JTable
JTable  Values to be displayed in JTextfield when Clicked on JTable Cells
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
Display Sum of Table Column Using In JSP
Display Sum of Table Column Using In JSP... an application to Display the sum data of a Table column for a specific Date. We created five file sum_count.jsp, resultForm.jsp, result_count.jsp, sumcount.java
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
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, 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
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 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
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
JTable
JTable  need to add values to a JTable having 4 coloumns ,2 of them are comboboxes
How to Sort Column values of a jsp table - JSP-Servlet
How to Sort Column values of a jsp table  Hi Friend Please help me. I am displaying reultset values on multiple jsp page. I want to sort selected column values on every page onclick of an event its very urgent pleasehelp
sum
sum  a program to find the sum of the alternative diagit of it ex- no=123456 sum=1+3+5=9
Shading Columns in JTable
; the rows in JTable. So, you are now capable for setting the shading the column... components. Then you have to get a colorful column of JTable. Here is the code... Shading Columns in JTable     
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
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
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
SQL All Column Names
SQL All Column Names       SQL All Column Names is useful when you want to see the Field, Type, Null... The Tutorial briefs you on 'SQL All Column Names'. To understand and grasp the example
sum
; sum=sum+s; if(i==n){ System.out.print(s...+"+"); } System.out.print("\nSum of the series : "+sum
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
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  Hello, i cannot display data from my table in the database to the cells of my JTable. please help me
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  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
how to get a column values from a excel file after attaching it - JSP-Servlet
how to get a column values from a excel file after attaching it  hi sir, How to get a column values from a excel file after attaching it while... and get a values from that store that value in To input box.Please help me sir
Create a Custom Cell Renderer in a JTable
cell renderer in a JTable component. Here, first of all you will know about... that can be used to display the cells in a column. The JTable describes a specific renderer to the particular column. For this the JTable invokes the table model
JTable
"}; JTable table=new JTable(data,labels); JScrollPane pane=new JScrollPane
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

Ads