Java自动计算表格某一数字列的和(2)

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;

/**
 * 自动计算表格某一数字列的和(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("数字列");
		Vector<Vector<String>> tableValues = new Vector<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("数字列总和:");
		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("自动计算表格某一数字列的和(2)");
		frame.pack();
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值