I was wondering how do I get just the JLabel image name rather than the whole path, I try to use
jLabel.getIcon();
but it uses the whole path rather than just the name (ie of name, berries.png). When I use getIcon(), the resuult is:
file:/C:/Users/lolne/Desktop/FCaWfD%20-%20Desktop%20Updated%20Version/build/classes/resources/quiz/berries.png
and I only wish for the ending part of berries.png to found. I am using this code for a food web game for a school assignment and this is the code where it is used:
public void checkResults(){
JLabel[] consumers = {
consumer1,
consumer2,
consumer3,
consumer4,
consumer5,
};
switch(RandomNumber.randomNumber){
case 1:
if(producer.getIcon().equals(producers[0])){
correct++;
}else{
producer.setBorder(BorderFactory.createLineBorder(Color.RED));
}
for(int i = 0; i < 5; i++){
if(consumers[i].getIcon().equals(consumers1.get(i))){
correct++;
}else{
consumers[i].setBorder(BorderFactory.createLineBorder(Color.RED));
}
}
break;
case 2:
if(producer.getIcon().equals(producers[1])){
correct++;
}else{
producer.setBorder(BorderFactory.createLineBorder(Color.RED));
}
for(int i = 0; i < 5; i++){
if(consumers[i].getIcon().equals(consumers2.get(i))){
correct++;
}else{
consumers[i].setBorder(BorderFactory.createLineBorder(Color.RED));
}
}
break;
case 3:
if(producer.getIcon().equals(producers[2])){
correct++;
}else{
producer.setBorder(BorderFactory.createLineBorder(Color.RED));
}
for(int i = 0; i < 5; i++){
if(consumers[i].getIcon().equals(consumers3.get(i))){
correct++;
}else{
consumers[i].setBorder(BorderFactory.createLineBorder(Color.RED));
}
}
break;
}
}
All in all my question is, "How do I get the name of the image itself rather than the whole path of the image"
you can use file getName() method to just get the name of the file
File file = new File("C:/Users/lolne/Desktop/FCaWfD%20-%20Desktop%20Updated%20Version/build/classes/resources/quiz/berries.png");
System.out.println(file.getName());
will output
berries.png