How to Add an Image in JFrame

Hello, folks!. In this tutorial, we will learn and focus on how to add an image in Java JFrame.

Create a Java JFrame

Firstly, we import the Java AWT and Java Swing libraries into our code. Secondly, we use the Java Swing library for the creation of JFrame. Furthermore, we use various components and methods inside the Swing library to achieve this. Thus, to get more information about Java AWT, Java Swing, their fields, construction, methods, etc., please do visit the below posts.

As a result, after reading the posts you will learn a lot about various Java libraries and use them with ease. We will use the below code for JFrame creation:

Let’s look at the code:

JFrame frame = new JFrame(); //JFrame Creation       
frame.setTitle("Add Image"); //Add the title to frame
frame.setLayout(null); //Terminates default flow layout
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Terminate program on close button
frame.setBounds(100, 200, 350, 300); //Sets the position of the frame
frame.setVisible(true); // Exhibit the frame

Output:

How to Add an Image in JFrame

Now, we have created a JFrame with a plain background. We must now add an image to the JFrame. Sadly, there is no inbuilt method in JFrame to do this. However, we will achieve it by using JLabel and performing our desired action.

Adding an Image in Java JFrame

Firstly, we create a JLabel using the Java Swing library. Secondly, we use the setIcon() method to add and display the image. This method defines to display the icon. However, if the value of the icon is null, nothing is displayed. We will now convert the image to an icon and pass this as a parameter to the setIcon() method.

We use the ImageIcon() class to convert the image to icons. This method is an implementation of the Icon Interface to retrieve icons from images. The internal functioning of this method is that it creates an array of byte streams which is read from the supported image file types using getResourceAsStream() class. Furthermore, the array of bytes gets converted to an Image Icon.

Let’s look at the code:

//Java Program to Add Image in Jframe 
import javax.swing.*;
import java.awt.*;

public class AddImage extends JFrame {
    public static void main(String[] args) {

        JFrame frame = new JFrame(); //JFrame Creation       
        frame.setTitle("Add Image"); //Add the title to frame
        frame.setLayout(null); //Terminates default flow layout
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Terminate program on close button
        frame.setBounds(100, 200, 350, 300); //Sets the position of the frame
        
        Container c = frame.getContentPane(); //Gets the content layer

        JLabel label = new JLabel(); //JLabel Creation
        label.setIcon(new ImageIcon("CodeSpeedy.jpg")); //Sets the image to be displayed as an icon
        Dimension size = label.getPreferredSize(); //Gets the size of the image
        label.setBounds(50, 30, size.width, size.height); //Sets the location of the image
 
    c.add(label); //Adds objects to the container
        frame.setVisible(true); // Exhibit the frame

    }
}

Output:

How to Add an Image in JFrame

Quick Analysis :

We pass the “CodeSpeedy.jpg” image location to the ImageIcon() method, where the image gets converted to a byte array. The array now gets converted to an Icon. Now, we add the Image Icon to our JFrame using the setIcon() method. We can also set the location and dimensions of the image using the setBounds() method and Dimension class respectively. Therefore, the image is now displayed on the JFrame.

Thanks for reading. I hope you found this post useful!.

Leave a Reply

Your email address will not be published.