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



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

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


ashwathy Mohan
sum of all values in a column of jtable
1 Answer(s)      2 years and 4 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
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 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
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
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
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
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
sum
; sum=sum+s; if(i==n){ System.out.print(s...+"+"); } System.out.print("\nSum of the series : "+sum
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
 

 

 

DMCA.com