Java automatically calculates the sum of a numeric column in a table

Source: Internet
Author: User


[Java]
Package com. han;
 
Import java. awt. BorderLayout;
Import java. awt. Container;
Import java. util. Vector;
 
Import javax. swing. JFrame;
Import javax. swing. JLabel;
Import javax. swing. JOptionPane;
Import javax. swing. JPanel;
Import javax. swing. JScrollPane;
Import javax. swing. JTable;
Import javax. swing. JTextField;
Import javax. swing. event. TableModelEvent;
Import javax. swing. event. TableModelListener;
Import javax. swing. table. DefaultTableModel;
 
/**
* Automatically calculates the sum of a numeric column in a table.
* @ Author HAN
*
*/
Public class TabelModelEvent_2 extends JFrame {
 
/**
*
*/
Private static final long serialVersionUID = 7045416728019138021L;
 
Float sum = 0;
 
Public TabelModelEvent_2 (){
// TODO Auto-generated constructor stub
Final Container container = getContentPane ();
JScrollPane scrollPane = new JScrollPane ();
Vector <String> tableNames = new Vector <String> ();
TableNames. add ("Number Column ");
Vector <String> tableValues = new Vector <String> ();
For (int I = 0; I <4; I ++ ){
Vector <String> vector = new Vector <String> ();
Vector. add (null );
TableValues. add (vector );
}
Final DefaultTableModel defaultTabelModel = new DefaultTableModel (
TableValues, tableNames );
JTable table = new JTable (defaultTabelModel );
ScrollPane. setViewportView (table );
Container. add (scrollPane, BorderLayout. CENTER );
JPanel panel = new JPanel ();
JLabel label = new JLabel ("total number columns :");
Final JTextField textField = new JTextField (10 );
Panel. add (label );
Panel. add (textField );
Container. add (panel, BorderLayout. SOUTH );
 
DefaultTabelModel. addTableModelListener (new TableModelListener (){
 
@ Override
Public void tableChanged (TableModelEvent e ){
// TODO Auto-generated method stub
If (e. getType () = TableModelEvent. UPDATE ){
Int row = e. getFirstRow ();
Int column = e. getColumn ();
 
// Keep that every cell value is float or null.
Try {
Float. parseFloat (String) defaultTabelModel. getValueAt (
Row, column ));
} Catch (NumberFormatException e1 ){
JOptionPane. showMessageDialog (container,
"Your input object is not a float type ");
DefaultTabelModel. setValueAt (null, row, column );
} Catch (NullPointerException e2 ){
// Do nothing
}
 
For (int I = 0; I <4; I ++ ){
Object cellValue = defaultTabelModel. getValueAt (I, 0 );
If (cellValue! = Null ){
 
Float floatValue = Float
. ParseFloat (String) cellValue );
Sum + = floatValue;
 
}
}
}
TextField. setText (new Float (sum). toString ());
Sum = 0;
}
 
});
}
 
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
TabelModelEvent_2 frame = new TabelModelEvent_2 ();
Frame. setTitle ("automatically calculate the sum of a numeric column in a table ");
Frame. pack ();
Frame. setVisible (true );
Frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
}
 
}

Package com. han;

Import java. awt. BorderLayout;
Import java. awt. Container;
Import java. util. Vector;

Import javax. swing. JFrame;
Import javax. swing. JLabel;
Import javax. swing. JOptionPane;
Import javax. swing. JPanel;
Import javax. swing. JScrollPane;
Import javax. swing. JTable;
Import javax. swing. JTextField;
Import javax. swing. event. TableModelEvent;
Import javax. swing. event. TableModelListener;
Import javax. swing. table. DefaultTableModel;

/**
* Automatically calculates the sum of a numeric column in a table.
* @ Author HAN
*
*/
Public class TabelModelEvent_2 extends JFrame {

/**
*
*/
Private static final long serialVersionUID = 7045416728019138021L;

Float sum = 0;

Public TabelModelEvent_2 (){
// TODO Auto-generated constructor stub
Final Container container = getContentPane ();
JScrollPane scrollPane = new JScrollPane ();
Vector <String> tableNames = new Vector <String> ();
TableNames. add ("Number Column ");
Vector <String> tableValues = new Vector <String> ();
For (int I = 0; I <4; I ++ ){
Vector <String> vector = new Vector <String> ();
Vector. add (null );
TableValues. add (vector );
}
Final DefaultTableModel defaultTabelModel = new DefaultTableModel (
TableValues, tableNames );
JTable table = new JTable (defaultTabelModel );
ScrollPane. setViewportView (table );
Container. add (scrollPane, BorderLayout. CENTER );
JPanel panel = new JPanel ();
JLabel label = new JLabel ("total number columns :");
Final JTextField textField = new JTextField (10 );
Panel. add (label );
Panel. add (textField );
Container. add (panel, BorderLayout. SOUTH );

DefaultTabelModel. addTableModelListener (new TableModelListener (){

@ Override
Public void tableChanged (TableModelEvent e ){
// TODO Auto-generated method stub
If (e. getType () = TableModelEvent. UPDATE ){
Int row = e. getFirstRow ();
Int column = e. getColumn ();

// Keep that every cell value is float or null.
Try {
Float. parseFloat (String) defaultTabelModel. getValueAt (
Row, column ));
} Catch (NumberFormatException e1 ){
JOptionPane. showMessageDialog (container,
"Your input object is not a float type ");
DefaultTabelModel. setValueAt (null, row, column );
} Catch (NullPointerException e2 ){
// Do nothing
}

For (int I = 0; I <4; I ++ ){
Object cellValue = defaultTabelModel. getValueAt (I, 0 );
If (cellValue! = Null ){

Float floatValue = Float
. ParseFloat (String) cellValue );
Sum + = floatValue;

}
} Www.2cto.com
}
TextField. setText (new Float (sum). toString ());
Sum = 0;
}

});
}

/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
TabelModelEvent_2 frame = new TabelModelEvent_2 ();
Frame. setTitle ("automatically calculate the sum of a numeric column in a table ");
Frame. pack ();
Frame. setVisible (true );
Frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
}

}


Author: Gaowen_HAN
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.