Article View: borland.public.jbuilder.java.swing
Article #493Re: Button/Icon problem
From: Lori M Olson [TeamB]
Date: Tue, 19 Jul 2005 14:34
Date: Tue, 19 Jul 2005 14:34
89 lines
3266 bytes
3266 bytes
Bodhi wrote:
> Hi,
>
>
>
> I've been trying to solve my problem for days now, but with no luck. I'm new
> to Swing programming, but as far as I can tell, my code should be ok. (See
> below).
>
>
>
> The problem is that I'm trying to create a button with an icon, but every
> time I end up with a button without an icon. I've added the image in the
> project view (it's also physically located in the same directory as the java
> file) and double clicking the icon name shows the image in JBuilder. So as
> far as I understand, there should not be a "path" or location problem.
>
>
>
> Running the getImageLoadStatus() method returns a code 4, which to my
> understanding indicates a loading problem.
>
>
>
> If anyone can give me a clue of how to solve my problem, it would be greatly
> appreciated.
>
>
>
> Sincerely,
>
> B.
>
>
> package icons;
>
> import javax.swing.*;
>
> public class Icons extends JFrame {
> ImageIcon icon = new ImageIcon("3dman.gif");
> JButton button = new JButton(icon);
> JPanel pane = new JPanel();
>
> public Icons() {
> setSize(400, 300);
> setTitle("Icon Test Frame");
> setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> //System.out.println(icon.getImageLoadStatus());
> pane.add(button);
> setContentPane(pane);
> show();
> }
>
> public static void main(String[] arguments) {
> Icons ike = new Icons();
>
> }
> }
>
>
It can't find the file "3dman.gif". You need to understand how it would
find that file, if you are going to load it that way.
I prefer loading images as resources, and then making sure they are in
the classpath.
The Java Tutorial covers this topic well:
http://java.sun.com/docs/books/tutorial/uiswing/misc/icon.html
--
Regards,
Lori Olson [TeamB]
------------
Save yourself, and everyone else, some time and search the
newsgroups and the FAQ-O-Matic before posting your next
question.
Google Advanced Newsgroup Search
http://www.google.ca/advanced_group_search
Other Newsgroup Searches:
http://www.borland.com/newsgroups/ngsearch.html
Joi Ellis's FAQ-O-Matic:
http://www.visi.com/~gyles19/fom-serve/cache/1.html
Technical Details
Message-ID:
<42dd0f7f$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!news-out.nntp.be!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<42dcdd58$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 111d45,35ae0302d2702117 X-Google-Attributes: gid111d45,public X-Google-Language: ENGLISH,ASCII-7-bit Date: Tue, 19 Jul 2005 08:34:41 -0600 From: "Lori M Olson [TeamB]" <javadragon@techie.com> User-Agent: Mozilla Thunderbird 1.0.2 (Macintosh/20050317) X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: borland.public.jbuilder.java.swing Subject: Re: Button/Icon problem References: <42dcdd58$1@newsgroups.borland.com> In-Reply-To: <42dcdd58$1@newsgroups.borland.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 142.179.181.200 Message-ID: <42dd0f7f$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1121783679 142.179.181.200 (19 Jul 2005 07:34:39 -0700) Lines: 89 Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!news-out.nntp.be!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail