[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