How To Calculate JTable Column Max Value In Java NetBeans
In this java Tutorial we will see How To Calculate JTable Column Max Value And Display It In JTextFields In Java NetBeans .
*Java Courses : Java Complete Course
Java Swing Course
*Java Courses : Java Complete Course
Java Swing Course
Project Source Code:
package java_tutorials;
import java.util.ArrayList;
import java.util.Collections;
/**
*
* @author 1BestCsharp
*/
public class Java_JTable_Column_Max_Value extends javax.swing.JFrame {
/**
* Creates new form Java_JTable_Column_Max_Value
*/
public Java_JTable_Column_Max_Value() {
initComponents();
getMax2();
}
// Method 1
public void getMax()
{
ArrayList<Integer> list = new ArrayList<Integer>();
for(int i = 0; i < jTable1.getRowCount(); i++)
{
list.add(Integer.parseInt(jTable1.getValueAt(i, 3).toString()));
}
int max = Collections.max(list);
jTextField1.setText(Integer.toString(max));
}
// Method 2
public void getMax2()
{
int max2 = 0;
for(int i = 0; i < jTable1.getRowCount(); i++)
{
int val = Integer.parseInt(jTable1.getValueAt(i, 3).toString());
if(val > max2)
{
max2 = val;
}
}
jTextField1.setText(Integer.toString(max2));
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jTextField1 = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTable1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{"1", "dcd", "ABC", "42"},
{"2", "ss", "rtyh", "14"},
{"3", "fs", "jhg", "62"},
{"4", "ynjhdg", "fg", "14"},
{"5", "uyt", "èiuy", "17"},
{"6", "nb", "oop", "26"},
{"7", "yui", "lkjh", "27"},
{"8", "rtyu", "jhg", "39"},
{"9", "jhgf", "dfg", "56"},
{"10", "gdsfe", "èiuytre", "46"}
},
new String [] {
"Id", "First Name", "Last Name", "Age"
}
));
jTable1.setRowHeight(30);
jScrollPane1.setViewportView(jTable1);
jTextField1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
jLabel1.setText("Max :");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(104, 104, 104)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 334, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
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(Java_JTable_Column_Max_Value.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Java_JTable_Column_Max_Value.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Java_JTable_Column_Max_Value.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Java_JTable_Column_Max_Value.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Java_JTable_Column_Max_Value().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
}
////////////////OutPut
Bagikan
Java JTable - How To Get JTable Column Maximum Value In Java NetBeans
4/
5
Oleh
insurance