tags:

views:

113

answers:

5

i want to display a image in my java application. i found a code to download the image from the webserver. what that code do is it takes the image and show it in the jframe.

i want to use a label to show the image or soemthing else. so i can put it in my java application. can someone help me. it shouldnt be JFrame

please help me

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

A: 

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
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
+1  A: 

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 Mangers. There are plenty of examples you can download and play with. Also you can nest different layout managers on different panels.

camickr
A: 

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
+1  A: 

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 Mangers.

trashgod
A: 

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.

DDP
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
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.)
DDP
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
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?
DDP