不知道我在这里做错了什么。我试图从我的目录中删除选定的文件,但它只是从列表中删除它。谢谢
private void deletecustButtonActionPerformed(java.awt.event.ActionEvent evt) {
DefaultListModel model = (DefaultListModel) customerList.getModel();
int selectedIndex = customerList.getSelectedIndex();
File customer = new File("Customers/" + selectedIndex);
if (selectedIndex != 1) {
customer.delete();
model.remove(selectedIndex);
}
}
最佳答案
int selectedIndex = customerList.getSelectedIndex();
我怀疑您想要获取 selectedIndex()。
我想你想获得选定的值:
String fileName = customerList.getSelectedValue().toString();
File customer = new File("Customers/" + fileName);
关于java - 从 Jlist 中删除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30020057/