日期:2014-05-20 浏览次数:20753 次
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
public class ExchangeDemo extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private Vector <Vector <Object>> data = new Vector <Vector <Object>>();
private static final Vector <String> COLUMN_NAMES = new Vector <String>();
private JTable table;
private int iCount = 0;
// 用来存储id和行的对应关系,看着方便一点
private HashMap <String, Integer> idToRowMap = new HashMap <String, Integer>();
DefaultTableCellRenderer render = new DefaultTableCellRenderer() {
@Override
public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus, int row,
int column) {
Component c = super.getTableCellRendererComponent(table, value,
isSelected, hasFocus, row, column);
if (row < 10) {
setBackground(row % 2 == 0 ? Color.green : Color.blue);
} else {
setBackground(Color.red);
}
return c;
}
};
public ExchangeDemo() {
super();
initData();
Container c = getContentPane();
table = new JTable();
table.setModel(new DefaultTableModel(data, COLUMN_NAMES) {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void setValueAt(Object value, int row, int column) {
Object oValue = getValueAt(row, column);
if (value.equals(oValue)) {
return;
}
if (column == 0) {
int iValue;
try {
iValue = Integer.parseInt(value.toString());
} catch (Exception e) {
// TODO: 这地方的判断小子自己做吧
e.printStackTrace();
return;
}
if (iValue - 1 >= data.size()) {
// 这地方通常好的软件有一些比较强的处理,我就先拿这个顶了
JOptionPane.showMessageDialog(table, "error");
return;
}
if (row < data.size() && column < COLUMN_NAMES.size()) {
Object oldValue = data.get(row).get(column);
data.get(row).set(column,
value == null ? "" : value.toString());
data.get(idToRowMap.get(String.valueOf(value))).set(
column, oldValue);
//
updateMapInfo(value, oldValue);
fireTableDataChanged();
}
} else if (column == 3) {