• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Helped needed: JBuilder 9 Display a .jpg image

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
M Miller
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic