我有一个从文件中读取记录并显示它们的表,并且有一个删除按钮,当用户选择一行并单击时,该行也会从表和文本文件中删除。
(更新)
public class Readuser_A extends AbstractTableModel {
String[] columns = { "Fname", "Lname", "Gender", "Date", "ID" };
ArrayList<String> Listdata = new ArrayList<String>();
String[][] Arraydata;
public Readuser_A() {
try {
FileReader fr = new FileReader("AllUserRecords.txt");
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
Listdata.add(line);
}
br.close();
Arraydata = new String[Listdata.size()][];
for (int i = 0; i < Listdata.size(); i++) {
Arraydata[i] = Listdata.get(i).split(" ");
}
} catch (IOException e) {
}
}
public void RemoveMyRow(int row){
Listdata.RemoveElement(row);
}
@Override
public String getColumnName(int colu) {
return columns[colu];
}
public int getRowCount() {
if (null != Arraydata) {
return Arraydata.length;
} else {
return 0;
}
}
public int getColumnCount() {
return columns.length;
}
public Object getValueAt(int rowIndex, int columnIndex) {
return Arraydata[rowIndex][columnIndex];
}
}
我的第二课:
public class ReaduserM_A {
final JLabel myLable = new JLabel();
public ReaduserM_A() {
final Readuser_A RU = new Readuser_A();
final JTable mytable = new JTable(RU);
final JFrame Uframe = new JFrame("All Users");
JButton DellButton = new JButton("Delete User");
DellButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (mytable.getSelectedRow() != -1) {
removeRow(mytable.getSelectedRow());
RU.fireTableRowsDeleted(mytable.getSelectedRow(),
mytable.getSelectedRow());
} else {
JOptionPane.showMessageDialog(null, "No Row Selected");
return;
}
//Now, Delete from text file too
deleteFromFile();
}
});
JPanel panel = new JPanel();
JScrollPane sp = new JScrollPane(mytable);
panel.add(sp);
panel.add(DellButton);
panel.add(myLable);
Uframe.add(panel);
Uframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Uframe.setSize(570, 500);
Uframe.setLocation(300, 60);
Uframe.setVisible(true);
}
public void deleteFromFile() {
File Mf = new File("AllUserRecords.txt");
File Tf = new File("Uoutput.txt");
try {
FileReader Ufr = new FileReader(Mf);
BufferedReader Ubr = new BufferedReader(Ufr);
PrintWriter Upw = new PrintWriter(new FileWriter(Tf));
String Us;
while ((Us = Ubr.readLine()) != null) {
String[] Ust = Us.split(" ");
String Unumber = Ust[4];
//How find the selected row line by it's ID and delete that row?
}
Upw.close();
Ubr.close();
Mf.delete();
Tf.renameTo(Mf);
} catch (FileNotFoundException e1) {
myLable.setText("File Not Found");
} catch (IOException ioe) {
myLable.setText("IO Error");
ioe.printStackTrace();
}
}
public static void main(String[] args) {
new ReaduserM_A();
}
}
谢谢