如题,请大家帮看下哪里有问题,很急的,请大家帮帮忙,先谢过了。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class TableTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Test();
}
public static void Test() {
JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(300, 240));
frame.setLayout(new BorderLayout());
String[] columns = { "类型", "值" ,"用于选中行"};
final DefaultTableModel tableModel = new DefaultTableModel(columns, 0);
final CustTable custTable = new CustTable();
custTable.setModel(tableModel);
JScrollPane scrollPane = new JScrollPane(custTable);
scrollPane.setPreferredSize(new Dimension(300, 150));
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
custTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
custTable.getColumnModel().getColumn(0).setPreferredWidth(111);
custTable.getColumnModel().getColumn(1).setPreferredWidth(111);
custTable.getColumnModel().getColumn(1).setPreferredWidth(111);
String[] types = { "", "数字", "字母" };
JComboBox typesComboBox = new JComboBox(types);
custTable.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(typesComboBox));
custTable.getModel().addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
// TODO Auto-generated method stub
int row = e.getLastRow();
int column = 0;
Object data = custTable.getModel().getValueAt(row, column);// 报错行
if (data.equals("数字")) {
custTable.setCellEditor(row, 1, new CustCellEditor(new String[]{"1","2","3"}));
}
else if (data.equals("字母")){
custTable.setCellEditor(row, 1, new CustCellEditor(new String[]{"a","b","c"}));
}
else {
custTable.setCellEditor(row, 1, new CustCellEditor(new String[] {""}));
}
}
});
JButton addButton = new JButton("添加行");
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
tableModel.addRow(new Object[]{"","",""});
custTable.setModel(tableModel);
custTable.updateUI();
}
});
JButton deleteButton = new JButton("删除选中行");
deleteButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int[] selections = custTable.getSelectedRows();
// if (selections[selections.length-1] == custTable.getRowCount()-1) {
// System.out.println("最后一行不能删除,否则会造成数组越界");
// }
// else {
for (int i = selections.length-1; i >= 0; i--) {// 从后往前删
tableModel.removeRow(selections[i]);
}
custTable.setModel(tableModel);
custTable.updateUI();
// }
}
});
JPanel panel = new JPanel();
panel.add(addButton);
panel.add(deleteButton);
frame.pack();
frame.setLocationRelativeTo(null);
frame.add("Center", scrollPane);
frame.add("South",panel);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
// 定制表格,设置指定单元格的编辑器
class CustTable extends JTable
{
private static final long serialVersionUID = 1L;
int myRow = -1, myCol = -1;
TableCellEditor myEditor;
public void setCellEditor(int row, int column, TableCellEditor cellEditor) {
this.myRow = row;
this.myCol = column;
this.myEditor = cellEditor;
}
@Override
public TableCellEditor getCellEditor(int row, int column)
{
if (row == myRow && column == myCol && myEditor != null)
return myEditor;
return super.getCellEditor(row, column);
}
}
// 设置编辑器为下拉列表
class CustCellEditor extends DefaultCellEditor
{
private static final long serialVersionUID = 1L;
public CustCellEditor(String[] items) {
super(new JComboBox(items));
}
}
------解决思路----------------------
public void tableChanged(TableModelEvent e) {
int row = e.getLastRow();
if(row>=custTable.getModel().getRowCount()){
return;
}