0

I'm a student currently studying Java Programming and using Netbeans to create the application. The program is already done and loads nicely in the IDE (with images). I have to build it into JAR for a presentation to my lecturer and has done it but the images are not present in the JAR.

First of all, I've checked all the available answers for allowing images to be present in the JAR but I couldn't get it right with the program as it doesn't load even in IDE and shows error. Which most have pointed out that I have to input (getClass().getClassLoader().getResource("image.jpg")). I tried inputting it but it shows errors, mostly because my codes for placing the ImageIcon are different.

Below is my full code of the JFrame presenting the program:

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.*;
import javax.swing.ImageIcon;
import java.util.logging.Level;
import java.util.logging.Logger;
import static javax.swing.JFrame.EXIT_ON_CLOSE;

public class A2{

     public void GUI () throws IOException  { 

      JFrame frame = new JFrame();
      frame.setVisible(true); 
      frame.setTitle("Minus");
      frame.setSize(700,500);
      frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

           //Set the layout
          JPanel panel;
      panel = new JPanel();
      panel.setLayout(null); 
      frame.setContentPane(panel);

      // Create all components
      JLabel ctgy = new JLabel("Minus");
          JLabel minus2 = new JLabel("What is 6 squares minus 3 squares?");
      JButton ans_a = new JButton("9");
          JButton ans_b = new JButton("3");
          JButton ans_c = new JButton("7"); 
      JButton back = new JButton("Back");
          JLabel min2 = new JLabel();

      //Add objects to layout
      panel.add(ctgy);
          panel.add(minus2);
      panel.add(min2);
          panel.add(ans_a);
          panel.add(ans_b);
          panel.add(ans_c);
      panel.add(back);

      // Set position of objects in content pane
          min2.setLocation(100,100);
          minus2.setLocation(20,50);
      ctgy.setLocation(10,3);
          ans_a.setLocation(500,100);
          ans_b.setLocation(500,150);
          ans_c.setLocation(500,200);
      back.setLocation(500, 400);

      //Set size of object
          min2.setSize(300,300);
      ctgy.setSize(200,50);
          minus2.setSize(350,50);
          ans_a.setSize(100,30);
          ans_b.setSize(100,30);
          ans_c.setSize(100,30);
      back.setSize(100, 30);

      //Set the fonts and colors
      Font font1 = new Font("Cooper Black", Font.BOLD, 26);
      Font font2 = new Font("Calisto MT", Font.BOLD, 20);
      ctgy.setFont(font1);
      ctgy.setBackground(Color.white);
          minus2.setFont(font2);
          minus2.setBackground(Color.red);
      panel.setBackground (Color.RED);
          ans_a.setBackground(Color.white);
      ans_b.setBackground(Color.white);
          ans_c.setBackground(Color.white);

          min2.setIcon(new ImageIcon("src/images/6-3.png"));  
          ans_b.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent arg0) {
                JOptionPane.showMessageDialog(null, "Correct!");
                try {
                    A3.main(null);
                } catch (IOException ex) {
                    Logger.getLogger(A1.class.getName()).log(Level.SEVERE, null, ex);
                }
                   }
                });

            ans_a.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent arg0) {
                    JOptionPane.showMessageDialog(null, "Incorrect! Please try again."); 
                }
            });         

            ans_c.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent arg0) {
                    JOptionPane.showMessageDialog(null, "Incorrect! Please try again.");
                }
            });
         frame.repaint();



         back.addActionListener(new ActionListener () {
                        public void actionPerformed(ActionEvent f){
                                Categories.main(null);
                        }  
                 });



    }    

 public static void main (String [] args) throws IOException {
      A2 gd = new A2();
      gd.GUI();
     }


}

This is the part of my code where I state the JLabel to put my image in which I named as min2:

JLabel ctgy = new JLabel("Minus");
          JLabel minus2 = new JLabel("What is 6 squares minus 3 squares?");
          JButton ans_a = new JButton("9");
          JButton ans_b = new JButton("3");
          JButton ans_c = new JButton("7"); 
          JButton back = new JButton("Back");
          JLabel min2 = new JLabel();

This is adding the panel for the JLabel:

panel.add(min2);

The size and location:

min2.setLocation(100,100);
min2.setSize(300,300);

Lastly the image itself and location:

min2.setIcon(new ImageIcon("src/images/6-3.png")); 

This part will show error because I set this to open a new Class when a user click the JButton so that would happen since you don't have the A3 Class:

public void actionPerformed(ActionEvent arg0) {
                    JOptionPane.showMessageDialog(null, "Correct!");
                    try {
                        A3.main(null);
                    } catch (IOException ex) {
                        Logger.getLogger(A1.class.getName()).log(Level.SEVERE, null, ex);
                    } 

I've checked the JAR file with WinRAR and confirmed that images folder and the images are inside. I wanted to post the screenshots but Imgur isn't working for me.

The pathfile for all images are inside src/images.

Please suggest what changes I need to make. Thank you and sorry if it has been asked too much.

  • Your JAR should not contain a src folder... And you should load resources using `getClass().getResourceAsStream(String path)`. See this question https://stackoverflow.com/questions/2343187/loading-resources-using-getclass-getresource – OneCricketeer Jun 01 '16 at 03:43
  • Though a more thorough detail of how resources are loaded is here. http://stackoverflow.com/a/676273/2308683 – OneCricketeer Jun 01 '16 at 03:47
  • *"I have to build it into JAR for a presentation to my lecturer and has done it but the **images are not present** in the JAR."* Are you sure? What is the output of `jar -tvf the.jar` from the command line? – Andrew Thompson Jun 01 '16 at 04:04
  • 1) `frame.setSize(700,500);` This should be `pack()` and should be immediately before `frame.setVisible(true);` which itself should be last. But that will only work as expected one another problem is fixed.. 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Jun 01 '16 at 04:07
  • Like all the answers this type of question `new ImageIcon("src/images/6-3.png")` is wrong `ImageIcon(String)` assumes that the `String` is a reference to a file on the disk, which embedded resources aren't. Instead you should be using `new ImageIcon(getClass().getResource("/images/6-3.png"))` – MadProgrammer Jun 01 '16 at 09:26

1 Answers1

0
jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/img.png")));

this is how you should actually load the image file.your given file path is wrong try it with this approach.

Priyamal
  • 2,919
  • 2
  • 25
  • 52