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

 
 


ashwathy Mohan
sum of all values in a column of jtable
1 Answer(s)      8 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.

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 Pages:
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
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
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
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
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
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
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
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
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
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
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
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
JTable
JTable  need to add values to a JTable having 4 coloumns ,2 of them are comboboxes
Sum - JSP-Servlet
Sum  Hi all, Please I need your help (source code) on how I can get the total sum of items from database using Java servlet or Jsp and html form. It should display the affected rows and the total sum of all in a seperate column
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
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
Sum of positive and negative numbers
Sum of positive and negative numbers  Hi if someone could help that would be great Write a program to read in 10 integer values and output the sum of all the positive integers and the sum of all the negative integers.  
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     
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
SQL Aggregate Sum
-set by column name'stu_id'. select stu_id, stu_name, sum(marks... SQL Aggregate Sum       The SQL Aggregate Sum Function is a part of Aggregate Function that is used
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
Creating a JTable Component
[]): This is the constructor of JTable class that implements a JTable for displaying the values in tabular format like rows and columns. All rows must have the same length as column... that will have to create a JTable. col - It specifies the name of each column
MySQL Addition
of the values of the column column_name. If there are no rows then SUM() returns NULL. The DISTINCT keyword can be used to sum only the distinct values of the column...; MySQL Addition returns you the sum value of a specific column for a group
Column select
Column select   How i fetch Experience wise resume?   Create a column experience which consist of only two values either yes...; OR Create a column experience which consist of integer values, shows how many
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
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
Setting Grid Line in JTable
. For setting the grid line, you will need a JTable. So, first of all this program creates a JTable including the data and columns. All the columns have... Setting Grid Line in JTable     
Creating a Scrollable JTable
helps you to create a scrollable JTable. First of all, this program creates..._COLUMN: It adjusts in the base of last column.  AUTO_RESIZE_ALL_COLUMNS: It resize all column automatically. Here is the code of program
jtable in java swing
entering the values in d 2nd column the user hit on the enter button.then the focus should reach to 2nd row 1st column. user can not able to edit the values present...jtable in java swing  hai friends... i am creating 1 GUI having 1
SQL Aggregate Sum
SQL Aggregate Sum       The SQL Aggregate Sum Function is a part of Aggregate Function that is used to compute sum of the numeric field in a table. Understand with Example
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
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
Sum of integers
Sum of integers  A Java program that reads an integer value from the user and displays i) the sum of all even integers between 1 and the input value, both inclusive. ii) The sum of all odd integers between 1 and the input
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
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 Display Data From MySQL Database
JTable Display Data From MySQL Database In this section we will discuss about... will describe you the displaying of data of a database table into a JTable... column header's name, how can you show data into the table. How to create
Sharing a Table Model between JTable Components
all resources to each other. When you change the values in the table model... a table model between JTable components. For this, first of all you will need... Sharing a Table Model between JTable Components
Calculate the Sum of three Numbers
Calculate the Sum of Three Numbers   ... . In this section you will learn how to calculate the sum of three numbers by using three... how to calculate three integer number . First of all define class name "
JTable Cell Validation? - Swing AWT
JTable Cell Validation?  hi there please i want a simple example of JTable with 3 columns Name,Number,Price and the first columns is string and second and third are integer and if you try to enter non numeric values
Setting the Height and Width of Rows and columns in JTable
all descriptions of a column in a JTable like: width, maximum and minimum width... that is used for displaying and editing the values in the column. getColumnModel... Setting the Height and Width of Rows and columns in JTable
Multi-line cells in the JTable - Java Tutorials
in the JTable The last slide of all my courses says that my students may...Multi-line cells in the JTable 2002-04-11 The Java Specialists' Newsletter [Issue 045] - Multi-line cells in the JTable Author: Dr. Heinz M. Kabutz
Swings JTable
Swings JTable  add values to JTable with four coloums,two of them are comboboxes
Jtable-Java
Jtable-Java  Hi all,I have a Jtable And i need to clear the data in the table .I only Need to remove the data in the table.not the rows.Please help me

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.