10

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(
        "http://www.personal.psu.edu/acr117/blogs/audrey/images/image-2.jpg");
    image = ImageIO.read(url);
} 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);
frame.setVisible(true);

Can someone help me?

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
Nubkadiya
  • 3,285
  • 13
  • 40
  • 45
  • 1
    Does your code work or not? Did you try it out? If it doesn't work, then what's wrong - do you get an error message, or does it not look like you expect? The more details you provide, the better people can help you. – Jesper May 31 '10 at 13:15
  • The code does exactly what you describe. The image is shown in the label lblimage. You can put the label in a JPane and it will work. What do you think is not working? – openCage May 31 '10 at 13:58
  • no its working fine. but it shows in frame. but i want this image to show in a label or something. so i can adjust the size and all. i want to put this image in a specific area of my application. please help me – Nubkadiya May 31 '10 at 14:12
  • The label is the only thing in the frame, so it takes up the whole frame. Once you add more stuff to your GUI it will look fine. – bcoughlan May 31 '10 at 14:35
  • i tried adding more and more labels. im just dragging dropping them. but only the image is displaying.. others are not showing – Nubkadiya May 31 '10 at 14:43
  • Reformatted code; please revert if incorrect. – trashgod May 31 '10 at 16:22

6 Answers6

4

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());
mainPanel.add(lblimage);
// add more components here
frame.add(mainPanel);
frame.setVisible(true);

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

Haldean Brown
  • 12,411
  • 5
  • 43
  • 58
  • i tried dragging dropping a panel and making this code adjust to that. but not working. and i want to add more and more items (buttons and labels ) to my GUI. can you please help me – Nubkadiya May 31 '10 at 14:48
3

You could try doing this instead:

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

I find it to be much easier to do :D

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.

exodrifter
  • 658
  • 1
  • 12
  • 23
  • whats my problem is this. i have already added a image in to my label. but its not displaying. the label is from drag and drop. so i want when my application runs the label should display the image. and the other thing is it should not take the whole GUI. using jframe what it do is it takes the whole frame so i cant even add any other buttons or labels – Nubkadiya May 31 '10 at 23:42
  • What do you mean that it should not take the whole GUI? And by drag and drop, do you mean you're using another program to make the GUI? If the image isn't showing up, what you'll probably have to do is put the JLabel containing the image into a JPanel and then tell the JPanel to execute updateUI(); (I think. I'm still not sure what your problem is.) – exodrifter Jun 01 '10 at 02:02
  • i have more buttons and labels. all of them are not created by the code. i have drag and drop them in to the form. – Nubkadiya Jun 01 '10 at 11:36
  • That's what's confusing; what program are you using to drag and drop them into the form? And what do you mean by form? Are you building a program to drag and drop components? – exodrifter Jun 01 '10 at 17:54
2

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.

Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

Try this:

JLabel lblNewLabel_1 = new JLabel("");
lblNewLabel_1.setIcon(new ImageIcon(MainMenu.class.getResource("/Images/Bugs.png")));
Deathstar
  • 75
  • 5
  • 13
1

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.

Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
camickr
  • 321,443
  • 19
  • 166
  • 288
1

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

OscarRyz
  • 196,001
  • 113
  • 385
  • 569