Java automatically calculates the sum of a numeric column in a table (2)

Source: Internet
Author: User


[Java]
Package com. han;
 
Import java. awt. BorderLayout;
Import java. awt. Container;
Import java. awt. event. MouseAdapter;
Import java. awt. event. MouseEvent;
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 calculate the sum of a numeric column in a table (2)
* @ Author HAN
*
*/
Public class TableModelEvent_2_Modified extends JFrame {
 

 
/**
*
*/
Private static final long serialVersionUID = 465495126108026319L;
Float sum = 0;
Object currentSelectedValue;
 
Public TableModelEvent_2_Modified (){
// 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 );
Final 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 );

Table. addMouseListener (new MouseAdapter (){
Public void mouseClicked (MouseEvent e ){
If (e. getButton () = MouseEvent. BUTTON1 ){
CurrentSelectedValue = table. getValueAt (table. getSelectedRow (), table. getSelectedColumn ());
}
}
});
 
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 ();
If (currentSelectedValue! = Null ){
Sum-= Float. parseFloat (String) currentSelectedValue );
}
// Keep that every cell value is float or null.
Try {
Sum + = Float. parseFloat (String) table. 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
}
}
TextField. setText (new Float (sum). toString ());
}
 
});
}
 
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
TableModelEvent_2_Modified frame = new TableModelEvent_2_Modified ();
Frame. setTitle ("automatically calculate the sum of a number column in the table (2 )");
Frame. pack ();
Frame. setVisible (true );
Frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
}
 
}

Package com. han;

Import java. awt. BorderLayout;
Import java. awt. Container;
Import java. awt. event. MouseAdapter;
Import java. awt. event. MouseEvent;
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 calculate the sum of a numeric column in a table (2)
* @ Author HAN
*
*/
Public class TableModelEvent_2_Modified extends JFrame {

 

/**
*
*/
Private static final long serialVersionUID = 465495126108026319L;
Float sum = 0;
Object currentSelectedValue;

Public TableModelEvent_2_Modified (){
// 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 );
Final 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 );

Table. addMouseListener (new MouseAdapter (){
Public void mouseClicked (MouseEvent e ){
If (e. getButton () = MouseEvent. BUTTON1 ){
CurrentSelectedValue = table. getValueAt (table. getSelectedRow (), table. getSelectedColumn ());
}
}
});

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 ();
If (currentSelectedValue! = Null ){
Sum-= Float. parseFloat (String) currentSelectedValue );
} Www.2cto.com
// Keep that every cell value is float or null.
Try {
Sum + = Float. parseFloat (String) table. 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
}
}
TextField. setText (new Float (sum). toString ());
}

});
}

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

}


 

Author: Gaowen_HAN

Related Article

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.