I've got a JComboBox filled with some java.io.File
objects. By selecting one of these files in the ComboBox, I want to delete it either from the ComboBox and Filesystem.
Code snippet:
deleteButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int dialogButton = JOptionPane.YES_NO_OPTION;
int dialogResult = JOptionPane.showConfirmDialog(null, "Are you sure?", "Warning", dialogButton);
if (dialogResult == JOptionPane.YES_OPTION)
{
Path path = Paths.get(mailingLists.getSelectedItem().toString());
mailingLists.removeItem(mailingLists.getSelectedItem());
try
{
Files.delete(path);
JOptionPane.showMessageDialog(null, "File deleted!", "SUCCESS", JOptionPane.INFORMATION_MESSAGE);
} catch (IOException e1)
{
JOptionPane.showMessageDialog(null, e1.toString(), "ERROR", JOptionPane.ERROR_MESSAGE);
e1.printStackTrace();
}
}
}
});
It gives this exception: java.nio.file.FileSystemException [...] file already in use
this is because it's used by my application, then I thought first to remove it from the ComboBox and then delete it using Files.delete(path);
but still have the exception.
What's wrong?
P.S.
Is the first time that I deal in this context so I guess if it's better to use File f = new File("path"); f.delete();
instead of Files.delete(path);
.
EDIT: Provided more information about the JComboBox load.
Scratch:
LinkedList<File> listFolder = new LinkedList<File>();
listFolder.add(new File("mailinglists"));//<--- root folder
File[] stuffInFolder = listFolder.get(0).listFiles();
JComboBox<File> mailingLists = new JComboBox<File>(stuffInFolder);