Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners sum of all values in a column of jtable

Have Programming Question? Ask it here!
 
 
 


ashwathy Mohan
sum of all values in a column of jtable
1 Answer(s)      a year and 11 months ago
Posted in : Java Beginners


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.


Advertisement
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
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
JTable duplicate values in row
JTable duplicate values in row  JTable duplicate values in row
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
in setting the column headers in JTable. First of all, program creates a table that has... Setting the Column Header in JTable   ... the creation of JTable without column headers. In this example you will learn
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... of that column of which we want to calculate the sum. If the statement gets processed
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
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
Removing a Column from a JTable
of TableColumn that represents all attributes of a column in a JTable.    ... Removing a Column from a JTable     ... from a JTable component that uses the table model. Removing a column from a JTable
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... of program: In this program, we will see how to add (append) a new column in 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 
Getting Cell Values in a JTable
; at a specified row and column position in JTable. The cell values display... Getting Cell Values in a JTable     ... values in a JTable component. It is a same as setting the cell values in a JTable
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
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
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 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
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
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
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... etc. Description of program: First of all this program creates a JTable
sum
sum  a program to find the sum of the alternative diagit of it ex- no=123456 sum=1+3+5=9
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  Values to be displayed in JTextfield when Clicked on JTable Cells
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
sum
; sum=sum+s; if(i==n){ System.out.print(s...+"+"); } System.out.print("\nSum of the series : "+sum
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 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
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  need to add values to a JTable having 4 coloumns ,2 of them are comboboxes
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
Shading Columns in JTable
; the rows in JTable. So, you are now capable for setting the shading the column.... Then you have to get a colorful column of JTable. Here is the code of program... 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 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
Find sum of all the elements of matrix using Java
Find sum of all the elements of matrix using Java A matrix is a rectangular... dimensional array. Now to determine the sum of all these elements, we have..., this variable get the sum of all the elements. Here is the code: import java.io.
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
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
JTable
JTable  Hello, i cannot display data from my table in the database to the cells of my JTable. please help me
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
Get All Keys and Values of the Properties files in Java
Get All Keys and Values of the Properties files in Java... how to get all keys and it's values of the properties files in Java. Java provides the features to collection of all keys and values of the properties file
Get All Keys and Values of the Properties files in Java
Get All Keys and Values of the Properties files in Java... to get all keys and values of the properties files in the Java. This section illustrates the way for getting all keys and corresponding values of the properties
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
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
Advertisements
 

 

 

DMCA.com