javaimagenetbeansimageiconjform

Pictures are not shown when I clean and run java project


I have a simple form that displays couple of pictures with the if statement. I have a folder called "Weather" in the project directory and it includes all the pictures that I use. When I run the project from NetBeans, everything is fine. However, when I do "Clean and Build Project" then run the exported jar file, the pictures are not shown. I couldn't figure it out why it happens. I've added the code that I use in case it's needed.

public void loadWeather() {
    Weather w = new Weather();
    lblWeatherCity.setText(w.getCity());
    lblWeatherTemp.setText(w.getTemp());
    lblWeatherCondition.setText(w.getStatus());

    String weatherCondition =(String) w.getStatus();
    String cloudy = "Cloudy";
    //System.out.println(weatherCondition);



    if(weatherCondition.contains(cloudy)){
        ImageIcon test = new ImageIcon("Weather/Cloudy.jpg"); 
        lblWeatherContitionIcon.setIcon(test);
        lblWeatherContitionIcon.setText(null);

    }else{
        ImageIcon test = new ImageIcon("Weather/academia_logo.jpg"); 
        lblWeatherContitionIcon.setIcon(test);
        lblWeatherContitionIcon.setText(null);

    }     

}

Solution

  • Have you added this directory as a resource to your project? When you start your project from inside Netbeans it may work, because the directory is found (as it exists relative to the application "start directory"). If you build a distribution Netbeans needs to know about the additional project resources (images, icons, localization files etc) - those resources will be added to the jar.

    Here you can find an article in the Netbeans knowledge base on how to work with images/ resources.