-1
This one-button action serves to add the path of a file into a JList
:
private void bArquivoActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.showOpenDialog(this);
File arquivo = fileChooser.getSelectedFile();
listaarquivos.add(arquivo);
tArquivo.setText(arquivo.getPath());
DefaultListModel modelo = new DefaultListModel();
tListaArquivo.setModel(modelo);
String l = arquivo.getPath();
if(listaarquivos.size()==1){
modelo.add(0,l);
index = 1;
}else{
modelo.add(index, l);
index++;
}
}
It shows the following error when the button is called a second time and so it goes to the else
:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 > 0
Please provide a [mcve] so that it is possible to execute the code and test the problem.
– user28595
Why not remove this if/Else and let java take care of the list size for you using
modelo.addElement(l)
?– user28595