我在没有数据库的java中创建了一个库系统程序。(直接使用文件)。
我在删除 jtable 中的一行(也从文件中删除)时遇到了一个奇怪的问题。
有时,当我在表格中选择一行并单击删除按钮时,不止一行已被删除!
大多数时候它也能正常工作!!
我的代码:
public final class UserPage extends JFrame implements ActionListener {
private AllUser userModel;
private JTable uTable;
JButton deleteUser;
int selectedRow;
public UserPage() {
titleUserCount();
userModel = new AllUser();
uTable = new JTable(userModel);
add(new JScrollPane(uTable), BorderLayout.CENTER);
add(buttonPanels(), BorderLayout.PAGE_START);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800, 600);
this.setLocation(300, 60);
this.setResizable(false);
}
public final JPanel buttonPanels() {
JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
deleteUser = new JButton("Delete User");
deleteUser.addActionListener(this);
buttonsPanel.add(deleteUser);
return buttonsPanel;
}
public void titleUserCount() {
AllUser userCount = new AllUser();
UserPage.this.setTitle("All User Information , Number Of user is : " + userCount.getRowCount());
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == deleteUser) {
int selectedrow = uTable.getSelectedRow();
if (selectedrow >= 0) {
userModel.RemoveRow(selectedrow);
titleUserCount();
} else {
JOptionPane.showMessageDialog(null, "No Row Selected");
}
}
}
}
我的模型课:
public class AllUser extends AbstractTableModel {
UserInformation uiS = new UserInformation();
String[] col = {"ID", "Fname", "Lname", "Gender", "Date"};
ArrayList<UserInformation> Udata = new ArrayList<UserInformation>();
public AllUser() {
BufferedReader br = null;
try {
FileReader fr = new FileReader("AllUserRecords.txt");
br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
if (line.trim().length() == 0) {
continue;
}
Udata.add(initializeUserInfos(line));
}
} catch (IOException e) {
} finally {
if (br != null) {
try {
br.close();
} catch (IOException ioe) {
}
}
}
}
private UserInformation initializeUserInfos(String str) {
UserInformation Uinit = new UserInformation();
String[] CellArray = str.split(" ");
Uinit.setID(CellArray[0]);
Uinit.setFname(CellArray[1]);
Uinit.setLname(CellArray[2]);
Uinit.setGender(CellArray[3]);
Uinit.setDate(CellArray[4]);
return Uinit;
}
public void RemoveRow(int rowIndex) {
if (RemoveUserFromFile(rowIndex)) {
Udata.remove(rowIndex);
fireTableRowsDeleted(rowIndex, rowIndex);
} else {
JOptionPane.showMessageDialog(null, "Unable to delete");
}
}
public boolean RemoveUserFromFile(int index) {
File Mf = new File("AllUserRecords.txt");
File Tf = new File("Uoutput.txt");
try {
BufferedReader Ubr = new BufferedReader(new FileReader(Mf));
PrintWriter Upw = new PrintWriter(new FileWriter(Tf));
String line;
while ((line = Ubr.readLine()) != null) {
if (line.trim().length() == 0) {
continue;
}
if (!line.startsWith(String.valueOf(getValueAt(index, 0)))) {
Upw.println(line);
}
}
Upw.close();
Ubr.close();
Mf.delete();
Tf.renameTo(Mf);
return true;
} catch (FileNotFoundException e1) {
return false;
} catch (IOException ioe) {
return false;
}
}
@Override
public String getColumnName(int colu) {
return col[colu];
}
@Override
public int getRowCount() {
return Udata.size();
}
@Override
public int getColumnCount() {
return col.length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
UserInformation uinfoS = Udata.get(rowIndex);
Object value = null;
switch (columnIndex) {
case 0:
value = uinfoS.getID();
break;
case 1:
value = uinfoS.getFname();
break;
case 2:
value = uinfoS.getLname();
break;
case 3:
value = uinfoS.getGender();
break;
case 4:
value = uinfoS.getDate();
break;
default:
value = "...";
}
return value;
}
@Override
public void setValueAt(Object value, int rowIndex, int columnIndex) {
UserInformation userInfo = Udata.get(rowIndex);
switch (columnIndex) {
case 0:
userInfo.setID((String) value);
break;
case 1:
userInfo.setFname((String) value);
break;
case 2:
userInfo.setLname((String) value);
break;
case 3:
userInfo.setGender((String) value);
break;
case 4:
userInfo.setDate((String) value);
break;
}
}
}
用户信息类:
public class UserInformation {
private String Fname;
private String Lname;
private String ID;
private String Gender;
private String Date;
public String getFname() {
return Fname;
}
public void setFname(String fname) {
this.Fname = fname;
}
public String getLname() {
return Lname;
}
public void setLname(String lname) {
this.Lname = lname;
}
public String getID() {
return ID;
}
public void setID(String i_d) {
this.ID = i_d;
}
public String getGender() {
return Gender;
}
public void setGender(String gndr) {
this.Gender = gndr;
}
public String getDate() {
return Date;
}
public void setDate(String date) {
this.Date = date;
}
@Override
public String toString() {
return ID + " " + Fname + " "
+ Lname + " " + Gender + " " + Date + "\n";
}
}
我的文本文件:
85 lo ii Female 2013/03/08
86 jkj nmn Female 2013/03/08
52 tyr fgfg Female 2013/03/08
7 dfdf wew Female 2013/03/08
47 zczc asa Female 2013/03/08
16 erw www Male 2013/03/08
83 gfg dsd Male 2013/03/08