aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Helped needed: JBuilder 9 Display a .jpg image Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Helped needed: JBuilder 9 Display a .jpg image " Watch "Helped needed: JBuilder 9 Display a .jpg image " New topic
Author

Helped needed: JBuilder 9 Display a .jpg image

M Miller
Greenhorn

Joined: Mar 02, 2004
Posts: 8
Hi there,
I'm new to JBuilder 9 and have had school experience with using JAVA.
I am trying to display a .jpg file.
I already have loaded it with:
-------------------------
Image image = null;
// Read from a file
File file = new File("C:\\FILES\\martian_girlfriend.jpg");
image = ImageIO.read(file);
--------------------------
How do I now display the loaded image? I already have gotten it to work
and load in a separate frame, but I am looking to load it into another
SWING component like JScollPane or other. This might not be possible
since I'm such a Newbie and do not know the full power of Swing.
So please send help.
Thanks,
Melanie.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

If you are using Swing, just load it in an ImageIcon and put the icon on a JLabel.



If you're using a JPEG, GIF, or PNG image, you can also just send the filename directly to the ImageIcon constructor. Once you've put the icon on the JLabel, you can put the JLabel in a JScrollPane if you wanted to.

If you're wanting to display an image in AWT, you'll need to subclass a component and override the paint() method. If you need to do this there are plenty of examples... just search the forum.


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
M Miller
Greenhorn

Joined: Mar 02, 2004
Posts: 8
Thanks Nate.
Late last night I also got it working but thanks for the scroll tip.
For other newbies, here's the code:
This will on pressing a button, popup an image in the JLabel field and also
pop an image up in a frame.

void jButton1_actionPerformed(ActionEvent e)
{
Image image = null;
ImageIcon icon=new ImageIcon
(Toolkit.getDefaultToolkit().createImage("C:\\FILES\\finger1.jpg"));
jLabel1.setIcon(icon);
this.repaint();

Image image1 = null;
try {
// Read from a file
File file = new File("C:\\FILES\\martian_girlfriend.jpg");
image1 = ImageIO.read(file);
} catch (IOException ee) {
}
// Use a label to display the image
JFrame frame = new JFrame();
JLabel label = new JLabel(new ImageIcon(image1));
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Helped needed: JBuilder 9 Display a .jpg image
 
Similar Threads
Problems loading image into a JLabel located in a JScrollPane
How to display a bitmap image ?
Image Dimensions
images not loading
Image display in layout manager