读书人

使用JTable在删除DefaultTableModel

发布时间: 2012-03-13 11:21:11 作者: rapoo

使用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() {


public void actionPerformed(ActionEvent e) {
JFileChooser jf = new JFileChooser();
int flag = jf.showOpenDialog(null);
if(flag==JFileChooser.APPROVE_OPTION)
{
File file = jf.getSelectedFile();
titles = new Vector();
titles.add( "姓名 ");
titles.add( "班级 ");
titles.add( "学号 ");
titles.add( "籍贯 ");

allrows = new Vector <String> ();



FileReader isr;
BufferedReader br;
try {
isr = new FileReader(file);
br = new BufferedReader(isr);
String s=null;
String[] str;
try {
while((s=br.readLine())!=null)
{
str = s.split( "\\| ");
vector = new Vector();
vector.add(str[0]);
vector.add(str[1]);
vector.add(str[2]);
vector.add(str[3]);
allrows.add(vector);


}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (FileNotFoundException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}


dtm = new DefaultTableModel();
dtm.setDataVector(allrows, titles);
table.setModel(dtm);
}
}
});
button_1.setText( "导入 ");
button_1.setBounds(125, 260, 72, 23);
getContentPane().add(button_1);

final JButton button_2 = new JButton();
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
button_2.setText( "导出 ");
button_2.setBounds(220, 260, 72, 23);
getContentPane().add(button_2);

final JButton button_3 = new JButton();
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
dtm.removeRow(row);}
});
button_3.setText( "删除 ");
button_3.setBounds(315, 260, 72, 23);
getContentPane().add(button_3);

name = new JTextField();
name.setBounds(121, 10, 84, 19);
getContentPane().add(name);


table = new JTable();
table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent arg0) {
int row = table.getSelectedRow();
String xm=table.getValueAt(row,0).toString();
String bj1 = table.getValueAt(row,1).toString();
String xh1 = table.getValueAt(row,2).toString();
String jg1 = table.getValueAt(row,3).toString();

name.setText(xm);
bj.setText(bj1);
id.setText(xh1);
jg.setText(jg1);
}


});
scrollPane.setViewportView(table);

}

}


[解决办法]
你就贴个代码出来。。。出现么事错误啊?
[解决办法]
删除报错是因为你删除的最后一笔数据,造成光标丢失,解决方法是删除最后一笔记录后手动换行到上一笔即可

读书人网 >J2SE开发

热点推荐