A
A
Alex_F1522020-09-08 10:30:43
Java
Alex_F152, 2020-09-08 10:30:43

Java swing. Can't add image and icon. How to be?

I made a launcher using Eclipse swing windowbuiler. After inserting the image using "absolute path". (It even said "Use only for quick testing, never use in real application". I inserted pictures for the Jframe icon itself, and as a simple picture as jlabel. Naturally, after compiling the jar file and opening it on another computer, there are no pictures. I understood that it does not load these pictures into the jar file itself. Googled for a long time. Transferred the pictures to the project folder. First in src, then simply indicated the path "src/image.jpg" and "/image.jpg", "image.jpg" "He just didn't see them. Then he created the img folder in parallel with src, threw them there, there was no effect. I tried to use

String iconpath1 = "1_QQL3nsLnggRfUCnldn0i7w (3).jpeg";
    File icon = new File(iconpath1); 
    ImageIcon imgicon = new ImageIcon(iconpath1);

The effect is still the same. The compiler simply does not see images. Tried with BufferedImage. And again the same effect. I know how to google, so I tried everything that was in Google. Probably an idiotic question. But how do you insert an image?
Project if anything on Maven assembler

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
mystifier, 2020-09-10
@Alex_F152

Pulled from a very old code:
ImageIcon icSave = new ImageIcon(SignalProcess.class.getResource("/resources/save64.png"));
Resources packed inside jar

S
Sergey Gornostaev, 2020-09-08
@sergey-gornostaev

First, you need to understand what a JAR is, how it is packaged, how it works, how runtime works with it, etc. Secondly, you need to familiarize yourself with the concept of "resource" and learn how to work with resources in Java. Thirdly, you need to read the Maven documentation on how it works with resources.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question