使用JTable,在删除DefaultTableModel数据的时候出现错误
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import
java.io.FileNotFoundException;
import java.io.FileReader;
import
java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
public class Demo extends JFrame {
final public JTextField name;
final public JTextField jg;
final public JTextField id;
final public JTextField bj;
public JTable table;
StringBuffer s = new StringBuffer();
StringBuffer sb;
Vector <String> vector;
DefaultTableModel dtm;
Vector titles;
Vector allrows;
/**
* Launch the application
* @param args
*/
public static void main(String args[]) {
try {
Demo frame = new Demo();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the frame
*/
public Demo() {
super();
getContentPane().setLayout(null);
setBounds(100, 100, 500, 375);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(98, 106, 302, 120);
getContentPane().add(scrollPane);
bj = new JTextField();
bj.setBounds(275, 10, 84, 19);
getContentPane().add(bj);
id = new JTextField();
id.setBounds(121, 47, 84, 19);
getContentPane().add(id);
jg = new JTextField();
jg.setBounds(275, 47, 84, 19);
getContentPane().add(jg);
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
int rows = table.getSelectedRowCount();
Vector vector= new Vector();
vector.add(name.getText());
vector.add(bj.getText());
vector.add(id.getText());
vector.add(jg.getText());
dtm.insertRow(row, vector);
dtm.removeRow(row+1);
}
});
button.setText( "更新 ");
button.setBounds(190, 77, 93, 23);
getContentPane().add(button);
final JButton button_1 = new JButton();
button_1.addActionListener(new ActionListener() {