Insert an image inside a jPanel in Java

As part of the process of creating an application, today I share a technique to insert an image into a jPanel in Java. The first thing I will do is create a new project in Java. I will keep the main class (Java Class) and add a jFrame which I will call Window. From the main class I will call the Frame upon execution.

Netbeans IDE

Now in the Frame we will add a Panel and a button. When we click the button an image will be added to the panel.

We create a package within the project and drag the image that we want to place inside the panel in the package. It can be dragged from any folder outside of Netbeans where we have it saved.

insertar una imagen dentro de un jPanel en Java

Now we go to the programming. Inside the Window code, we create a new class called Image in which we will place the following code:



With this class we have most of the programming ready. Now we go to the button that we will click to add the image to the panel and place the following:


With this we can execute our program and when we click on the button that we place, the image that we added will appear in the panel.

insert an image inside a jPanel in Java

That is all for now. If you have any questions, please use the comment box. Thanks.

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments