How To Calculate JTable Column Sum, Avarage, Max, Min Value In Java NetBeans
In this java Tutorial we will see How To Calculate JTable Column Sum - Average - Maximum - Minimum In Java NetBeans .
Project Source Code:
package javayt;
import java.util.ArrayList;
import java.util.Collections;
/**
*
* @author 1bestcsharp.blogspot.com
*/
public class JTableColumnTuTorial extends javax.swing.JFrame {
public JTableColumnTuTorial() {
initComponents();
Tsum.setText(Integer.toString(getSum()));
Taverage.setText(Float.toString(getAverage()));
getMaxMin();
}
// get the sum
public int getSum(){
int rowsCount = jTable1.getRowCount();
int sum = 0;
for(int i = 0; i < rowsCount; i++){
sum = sum+Integer.parseInt(jTable1.getValueAt(i, 2).toString());
}
return sum;
}
// get the Average
public float getAverage(){
float sum = getSum();
int rowsCount = jTable1.getRowCount();
float average = sum/rowsCount;
return average;
}
// get the maximum and the minimum
public void getMaxMin(){
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i = 0; i < jTable1.getRowCount(); i++){
list.add(Integer.parseInt(jTable1.getValueAt(i,2).toString()));
}
int max = Collections.max(list);
int min = Collections.min(list);
Tmax.setText(Integer.toString(max));
Tmin.setText(Integer.toString(min));
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
Tsum = new javax.swing.JTextField();
Taverage = new javax.swing.JTextField();
Tmax = new javax.swing.JTextField();
Tmin = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{"FN2", "LN2", "12"},
{"FN3", "LN3", "13"},
{"FN4", "LN4", "14"},
{"FN5", "LN5", "15"},
{"FN1", "LN1", "11"},
{"FN6", "LN6", "20"},
{"FN7", "LN7", "21"},
{"FN8", "LN8", "22"},
{"FN9", "LN9", "51"},
{"FN10", "LN10", "30"},
{"FN11", "LN11", "35"},
{"FN12", "LN12", "40"},
{"FN13", "LN13", "44"},
{"FN14", "LN14", "25"},
},
new String [] {
"First Name", "Last Name", "Age"
}
));
jTable1.setToolTipText("");
jScrollPane1.setViewportView(jTable1);
jLabel1.setFont(new java.awt.Font("Verdana", 1, 14)); // NOI18N
jLabel1.setText("Sum :");
jLabel2.setFont(new java.awt.Font("Verdana", 1, 14)); // NOI18N
jLabel2.setText("Average :");
jLabel3.setFont(new java.awt.Font("Verdana", 1, 14)); // NOI18N
jLabel3.setText("Max :");
jLabel4.setFont(new java.awt.Font("Verdana", 1, 14)); // NOI18N
jLabel4.setText("Min :");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 799, Short.MAX_VALUE)
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(Tsum, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Taverage, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Tmax, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(Tmin, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(205, 205, 205))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 282, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(26, 26, 26)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(Tsum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(23, 23, 23)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(Taverage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(25, 25, 25)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(Tmax, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(23, 23, 23)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(Tmin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(26, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(JTableColumnTuTorial.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(JTableColumnTuTorial.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(JTableColumnTuTorial.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(JTableColumnTuTorial.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JTableColumnTuTorial().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextField Taverage;
private javax.swing.JTextField Tmax;
private javax.swing.JTextField Tmin;
private javax.swing.JTextField Tsum;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}
Download Projects Source Code
2 comments
commentsyour channel youtube very usefull for me.. and i think, i can get more information about java netbeans from you. please share your contact with me.. thank you
ReplyThanks
Reply