Alright , I have a problem which I can't figure out whats wrong. So I have jList and List . I need a function that when I click on item (in jList any item ) and it would change in my label icon , (I'm dealing with images). It works somehow, it changes my label icon to the image I pick from jList, but it throws me Exception and the program crashes , usually first 2 items cause no errors , third and further items cause it. After it crashes and throws me bunch of red text , I can still change my icon.
This is the function where I get the images and add them to List (adding path to them)
private static void getImages(String src) throws IOException {
//Exctract the name of the image from the src attribute
int indexname = src.lastIndexOf("/");
if (indexname == src.length()) {
src = src.substring(1, indexname);
}
indexname = src.lastIndexOf("/");
String name = src.substring(indexname, src.length());
//Open a URL Stream
URL url = new URL(src);
InputStream in = url.openStream();
GPath=fPath+name;
OutputStream out = new BufferedOutputStream(new FileOutputStream( GPath));
//Im adding to the list string (link to image) here
imagesListN.add(GPath);
System.out.println("list size: "+imagesListN.size());
for (int b; (b = in.read()) != -1;) {
out.write(b);
}
out.close();
in.close();
}
It adds them normally . Yes I'm downloading them , that's why I want to see them once they are downloaded.
This is where I click on jList function.
list.addListSelectionListener(new ListSelectionListener() {
private int a;
@Override
public void valueChanged(ListSelectionEvent arg0) {
if (!arg0.getValueIsAdjusting()) {
String h;
int k;
k = list.getSelectedIndex();
System.out.println("List id: "+k);
a = Main.imagesListN.indexOf(k);
System.out.println("imagesListN id: "+a);
h = Main.imagesListN.get(k);
System.out.println("h : "+h);
ImageIcon img = new ImageIcon(h);
imageReview.setIcon(img);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
Here is the jList (name list) . Exception is at
a = Main.imagesListN.indexOf(k);
it gives me -1 , but the h = Main.imagesListN.get(k); gives me the link I need and gives it to ImageIcon img = new ImageIcon(h); and then imageReview.setIcon(img); . Label icon changes everytime when I click on item I need. Maybe it's not a = Main.imagesListN.indexOf(k); that I'm looking at , but something gives me -1. Btw I'm excecuting everything in Thread.
`public class Crawler extends Thread {
Main main = new Main();
public void run(){
try {
main.download();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}`
nothing fancy here. Each functions are in there own class , getImages() is in main , listListener is in class Langas (Class where are all buttons,labels , etc. are created nothing else) and Thread well , Thread. Also it works fine after everything is downloaded , no exceptions . Error appears durring downloading proccess
public void valueChanged(ListSelectionEvent e) { if (e.getValueIsAdjusting() == false) { if (list.getSelectedIndex() == -1) { //No selection, disable fire button. fireButton.setEnabled(false); } else { //Selection, enable the fire button. fireButton.setEnabled(true); } } }
– ibrahim demir Mar 11 at 11:27