Article View: borland.public.jbuilder.java.swing
Article #1263Re: Send help: JBuilder 9: display .jpg file with JBuilder using components.
From: Melanie Miller
Date: Wed, 03 Mar 2004 16:35
Date: Wed, 03 Mar 2004 16:35
68 lines
3118 bytes
3118 bytes
Yup.
Got it work late last night.
Here's the code for the other newsbies:
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);
}
}
"John McGrath [TeamB]" <ng-b@JPMcGrath.net> wrote in message
news:4045a1d1@newsgroups.borland.com...
> On 3/2/2004 at 3:51:23 PM, Melanie Miller wrote:
>
> > 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?
>
> The easiest way to display an image is using a JLabel. You could
> certainly use the code above to load the image, but there is an easier
> approach using the ImageIcon class:
>
> Icon icon = new ImageIcon( "C:\\FILES\\martian_girlfriend.jpg" );
> JLabel label = new JLabel( icon );
>
> Note that this assumes that you want to load the image from a *file*.
> If the image is a static part of your application, you will probably
> want to store the image along with your code (typically in a jar file),
> and then create the Icon using the ImageIcon(URL) constructor.
>
> --
> Regards,
>
> John McGrath [TeamB]
>
> ---------------------------------------------------
> Before sending me e-mail, please read:
> http://www.JPMcGrath.net/newsgroups/e-mail.html
Technical Details
Message-ID:
<4046099d$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<4044f413$1@newsgroups.borland.com> <4045a1d1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,5a2f575c0d58ef1d X-Google-Attributes: gid111d45,public X-Google-ArrivalTime: 2004-03-03 08:41:57 PST Reply-To: "Melanie Miller" <melanie.e.miller@lmco.com> From: "Melanie Miller" <melanie.e.miller@lmco.com> Newsgroups: borland.public.jbuilder.java.swing References: <4044f413$1@newsgroups.borland.com> <4045a1d1@newsgroups.borland.com> Subject: Re: Send help: JBuilder 9: display .jpg file with JBuilder using components. Date: Wed, 3 Mar 2004 11:35:33 -0500 Lines: 70 Organization: Self X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 NNTP-Posting-Host: 192.91.147.194 Message-ID: <4046099d$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1078331805 192.91.147.194 (3 Mar 2004 08:36:45 -0800) Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail