How to display an image in a Java application

I want to display an image in my Java application. I found a code to download the image from the webserver and show it in the jframe.

I want to use a label to show the image or something else. It shouldn’t be JFrame.

Here is my code:

Image image = null;
try {
    URL url = new URL(
    image =;
} catch (IOException e) {

// Use a label to display the image
JFrame frame = new JFrame();

JLabel lblimage = new JLabel(new ImageIcon(image));
frame.getContentPane().add(lblimage, BorderLayout.CENTER);
frame.setSize(300, 400);

Can someone help me?

Using the code you provided, you already have the image in a JLabel called lblimage. You can now add that to a panel or any other container object, like so:

JPanel mainPanel = new JPanel(new BorderLayout());
// add more components here

You can treat that label (lblimage) just like you would any normal component.

You could try doing this instead:

import javax.swing.ImageIcon;
JLabel image = new JLabel(new ImageIcon("imageName.png"));

I find it to be much easier to do 😀

EDIT: Upon reading the question for the 4th time, I realized that this is exactly what you did in the question. Now, I’m having trouble figuring out what exactly you’re asking for.

I would add two notes to the other useful answers:

1) Don’t swallow exceptions; write to a log or at least print a stack trace.

catch (IOException e) { e.printStackTrace(); }

2) Instead of writing frame.setSize(300, 400), use pack(), which “Causes this Window to be sized to fit the preferred size and layouts of its subcomponents.” In this way, the picture’s original dimensions will be used. This is also the foundation of @camickr’s suggestion to read about Layout Managers.

Your code already does what you need ( to display it in other than a JFrame – that’s a JLabel – )

For what you say in your comments, to try to drag and drop and add more components I think what you need is to use a GUI BUilder to achieve what you want ( which is not only show the image but add other components )

I would recommend you yo use IntelliJ IDEA’s GUI Builder

In order to have a complete overview on how Swing works and what can you with it, you can also take a look at: The swing tutorial

and i want to add more and more items
(buttons and labels ) to my GUI. can
you please help me

Your question has nothing to do with labels and images. You need to understand how to use Layout Managers to organize the components you add to a frame.

So start by reading the Swing tutorial on Layout Managers. There are plenty of examples you can download and play with. Also you can nest different layout managers on different panels.

Try this:

JLabel lblNewLabel_1 = new JLabel("");
lblNewLabel_1.setIcon(new ImageIcon(MainMenu.class.getResource("/Images/Bugs.png")));