1
I have a frame, with a list of values where several files are displayed.
At this time I am doing the delete button sentry (last part code), where the goal is to delete the file from disk, and jlist.
public void addContentFrame(){
frame.setLayout(new BorderLayout());
//lista de ficheiros - criada como instancia da classe
frame.add(listaFicheiros, BorderLayout.WEST);
File[] files = new File(diretoriaExecucao).listFiles(new FileFilter() {
@Override
public boolean accept(File f) {
return f.getName().endsWith(".rtf"); //so aceita ficheiros acabados em .rtf
}
});
// Meter valores aleatorios na lista
// model.addElement("A");
// model.addElement("B");
// model.addElement("C");
for(int i = 0; i < files.length; i++){ //como nao e array percorremos o tamanho por inteiro do array
model.addElement(files[i].getName());
}
//caixa texto onde vai ser apresentado o conteudo do ficheiro c/ ScrollArea
JTextArea caixaTexto = new JTextArea();
JScrollPane scrollArea = new JScrollPane(caixaTexto);
frame.add(scrollArea, BorderLayout.CENTER);
listaFicheiros.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
if(!e.getValueIsAdjusting()){
String valorSelecionado = (String) listaFicheiros.getSelectedValue();
//Abrir ficheiro selecionado e meter output numa JTextArea!!
try {
caixaTexto.setText(""); //limpo sempre a CT sempre que inicio a leitura de um ficheiro
BufferedReader br = new BufferedReader(new FileReader(diretoriaExecucao + "/" + valorSelecionado));
while(br.ready()){
String linha = br.readLine();
caixaTexto.append(linha + "\n"); //imprime continuamente as linhas na caixa texto
}
br.close(); //Ficheiro tem de ser sempre fechado
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
});
//botoes - parte inferior janela
JPanel painelBotoes = new JPanel();
painelBotoes.setLayout(new GridLayout(1,3));
JButton new1 = new JButton("new");
new1.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
String nomeFicheiroNovo = JOptionPane.showInputDialog(frame, "Qual o nome do novo ficheiro?");
//Criar um ficheiro novo e escrever para dentro dele
File ficheiroNovo = new File(diretoriaExecucao + "/" + nomeFicheiroNovo + ".rtf"); //dentro e a diretoria que queremos gravar ficheiro (em execucao)
//escrever para o ficheiro
try {
PrintWriter printWriter = new PrintWriter(ficheiroNovo); //ESCREVE
printWriter.println("Escreveu ficheiro?"); //O que queremos escrever no ficheiro.
model.addElement(ficheiroNovo.getName()); //adicionar ficheiro à lista para aparecer frame
printWriter.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
}
});
JButton delete = new JButton("delete");
//Passos: ver ficheiro selecionado, percorre lista, remove ficheiro disco e lista, atualizar lista de ficheiro(atualiza sozinha)
delete.addActionListener(new ActionListener (){
@Override
public void actionPerformed(ActionEvent e){
String ficheiroSelecionado = (String) listaFicheiros.getSelectedValue();
for(int i = 0; i < files.length; i++){
if(files[i].getName().equals(ficheiroSelecionado)){
files[i].delete();
model.removeElement(ficheiroSelecionado);
}
}
}
});
Although I can delete the file and the sentry to do her goal, the console still gives me the following error:
This error occurs on the line:
model.removeElement(ficheiroSelecionado);
I tested your code and didn’t get the message. I think it may be that when the
System.out.println()
tries to display the file, it has already been deleted.– user28595
@diegofm yes that’s right, it’s deleted. But even without the sysout it gives me the same error.
– rrr
@diegofm it gives error in model.removeElement(fileSelected). Do you think you should take it out of the model? Should you first take it out of the file array? and only after the model?
– rrr
You can display an example of your code that is [mcve]? As I said, I played only the current section and it worked normally without any message.
– user28595
he removed the file from the list, but says there is no path
– rrr
As I said, I reproduced exactly the current code of the question and no problems were detected. It can only be evaluated if you do as I suggested in the previous post.
– user28595
The code that is in the question is a file creation code. If the problem is in the deletion, the code is incorrect. That’s why @diegofm is not finding any errors.
– mutlei
I forgot to put the delete sentry, now this @mutlei
– rrr
Have tried to change the order of the two operations, to remove from the table before and remove the file after?
– mutlei
yes gives the same error
– rrr
I think then maybe the wrong operation is being used in the model. Try to use
removeRow(int rowId)
.– mutlei
As I have already said in previous comments, there is no problem with the code, except the deletion order (you must first delete the model object and then delete it physically), the changes are made correctly, that message is due to you trying to display something that has already been deleted as the
System.ou.println
was being called after thefile[i].delete()
.– user28595