我有一个表从文件中读取记录并显示它们 并且有一个删除按钮,当用户选择一行并单击时,该行也会从表和文本文件中删除。
(更新)
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();
}
}
谢谢
答案 0 :(得分:3)
removeRow(mytable.getSelectedRow());
以上陈述可能是问题所在。因为如果没有选定的行,则getSelectedRow
返回-1。
检查行是否存在。然后删除它是否存在。
if(mytable.getSelectedRow() != -1) {
removeRow(mytable.getSelectedRow());
}
<强>更新强>
我在NullPointerException
课程的getRowCount
方法中运行了TableModel
代码。
public int getRowCount() {
return Arraydata.length;
}
在计算之前,请进行null
检查。
public int getRowCount() {
if(null != Arraydata) {
return Arraydata.length;
} else {
return 0;
}
}
现在如果你跑步,你将获得索引为-1的ArrayOutOfBoundException
。这是因为删除操作。正如我之前所说,检查行是否存在然后执行相应的操作。以下代码就是这样做的。
public void actionPerformed(ActionEvent e) {
if(mytable.getSelectedRow() != -1) {
removeRow(mytable.getSelectedRow());
rftl2.fireTableRowsDeleted(mytable.getSelectedRow(), mytable.getSelectedRow());
} else {
JOptionPane.showMessageDialog(null, "No Row Selected");
return;
}
//Now, Delete from text file too
deleteFromFile();
}
最后得到输出(如果没有这样的选定行。)