Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / February 2005

Tip: Looking for answers? Try searching our database.

Sanity Check re JOptionPane.showMessageDialog

Thread view: 
Rhino - 06 Feb 2005 16:02 GMT
I am getting some odd behaviour with respect to the five-parameter version
of JOptionPane.showMessageDialog() in J2SE 1.5 on Windows XP: the icon I am
specifying does not appear in my JOptionPane.

I know that the icon is being found and loaded correctly; the icon is
definitely present and  not null in the debugger just before I invoke
showMessageDialog() so this *appears* to be a bug in showMessageDialog().
However, this method is pretty mature code and has been around since the
early days of Swing so I'm inclined to doubt that the code is buggy or it
would have been spotted long before now. I just checked the Bug Database and
couldn't find any other reports of this problem. That reinforces my
suspicion that it is not a bug in showMessageDialog(): this method must be
very widely used so if there was a bug in the method, surely hundreds of
people would have seen it by now.

That leads me to think I've done something wrong in my code, although I
certainly can't see a mistake. I was hoping someone here could try this code
and either:
a) verify that they are seeing this problem and it really is a bug -OR-
b) tell me what I've done wrong

If it is a real bug, I'll report it to the Bug Database.

To run my code and see the problem, all you need to do is supply the name of
a valid GIF, JPG, or PNG file in the argument of the 'getResource()' method
on the line that starts 'URL aboutIconUrl ='.

The three buttons illustrate different versions of showMessageDialog():
- 'About 1' uses the four-argument version of showMessageDialog(), the one
that uses the default icon, and works perfectly.
- 'About 2' uses the five-argument version of showMessageDialog() but has
the icon set to null; it simply displays the default icon and therefore has
the same result as 'About 1', which I assume is the correct result.
- 'About 3' demonstrates the problem because a valid non-default icon is
passed to the five-argument version of showMessageDialog() yet no icon, even
the default icon, is displayed in the dialog.

--TempAbout.java--------------------------------

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class TempAbout extends JFrame implements ActionListener {

   public static void main(String[] args) {

       TempAbout tempAbout = new TempAbout();
       tempAbout.pack();
       tempAbout.setVisible(true);
   }

   public TempAbout() {

       setLayout(new BorderLayout());
       JButton aboutButton1 = new JButton("About 1");
       aboutButton1.addActionListener(this);
       getContentPane().add(aboutButton1, BorderLayout.NORTH);

       JButton aboutButton2 = new JButton("About 2");
       aboutButton2.addActionListener(this);
       getContentPane().add(aboutButton2, BorderLayout.CENTER);

       JButton aboutButton3 = new JButton("About 3");
       aboutButton3.addActionListener(this);
       getContentPane().add(aboutButton3, BorderLayout.SOUTH);

   }

   public void actionPerformed(ActionEvent actionEvent) {

       String command = actionEvent.getActionCommand();
       if (command.equals("About 1")) {
           JOptionPane.showMessageDialog(null, "Message 1", "Title 1",
                   JOptionPane.INFORMATION_MESSAGE);
       } else if (command.equals("About 2")) {
           JOptionPane.showMessageDialog(null, "Message 2", "Title 2",
                   JOptionPane.INFORMATION_MESSAGE, null);
       } else if (command.equals("About 3")) {
           ImageIcon aboutIcon = null;
           URL aboutIconUrl = this.getClass().getClassLoader().getResource(
                   "Images/Duke.gif");
           if (aboutIconUrl == null) {
               System.out.println("Couldn't find icon file.");
           } else {
               /* convert URL to encoded String */
               String encodedAboutIconFileName = aboutIconUrl.getFile();
               String decodedAboutIconFileName = null;
               try {
                   decodedAboutIconFileName = URLDecoder.decode(
                           encodedAboutIconFileName, "UTF-8");
               } catch (UnsupportedEncodingException ue_excp) {
                   System.err.println("Failed to decode file name "
                           + encodedAboutIconFileName + ". Cannot
continue.");
                   System.exit(16);
               }
               aboutIcon = new ImageIcon(decodedAboutIconFileName);
           }

           JOptionPane.showMessageDialog(null, "Message 3", "Title 3",
                   JOptionPane.INFORMATION_MESSAGE, aboutIcon);
       }
   }
}

Signature

Rhino
---
rhino1 AT sympatico DOT ca
"There are two ways of constructing a software design. One way is to make it
so simple that there are obviously no deficiencies. And the other way is to
make it so complicated that there are no obvious deficiencies." - C.A.R.
Hoare

Roland - 06 Feb 2005 17:19 GMT
> I am getting some odd behaviour with respect to the five-parameter version
> of JOptionPane.showMessageDialog() in J2SE 1.5 on Windows XP: the icon I am
[quoted text clipped - 32 lines]
> passed to the five-argument version of showMessageDialog() yet no icon, even
> the default icon, is displayed in the dialog.

Works for me.
Windows 2000
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode)

Instead of decoding the URL back to a String, why don't you use
    aboutIcon = new ImageIcon(aboutIconUrl);

The only time I don't get an icon on the 3rd dialog, is when the image
is not a valid gif (I created a textfile boom.txt and renamed it to
boom.gif). If the image file cannot be found, the default message icon
gets displayed (but then I also see your message "Couldn't find icon
file.").
Signature

Regards,

Roland de Ruiter
  ___      ___
 /__/ w_/ /__/
/  \ /_/ /  \

Rhino - 06 Feb 2005 22:00 GMT
> Works for me.
> Windows 2000
> java version "1.5.0"
> Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
> Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode)

First, thanks *very* much for replying to this question. I am now satisfied
that there is no bug in showMessageDialog(), just as I had suspected.

> Instead of decoding the URL back to a String, why don't you use
>      aboutIcon = new ImageIcon(aboutIconUrl);

If I bypass the decoding, the line you suggested works fine MOST of the
time. However, if the file name includes an embedded blank, your suggestion
doesn't work.

I actually wrote this code a couple of years back and I don't recall exactly
why I added that decoding step but I *think* it was supposed to handle file
names that include imbedded blanks (or other whitespace?) characters.

Of course, if I execute the decoding, I *never* get the icon to display so
I'm further ahead following your suggestion ;-)

Since I expect at least the occasional file name that includes embedded
blanks, like "My About.gif", I think I need to search for a solution that
works all of the time, rather than assuming the file  names won't contain
embedded blanks.

> The only time I don't get an icon on the 3rd dialog, is when the image
> is not a valid gif (I created a textfile boom.txt and renamed it to
> boom.gif). If the image file cannot be found, the default message icon
> gets displayed (but then I also see your message "Couldn't find icon
> file.").

Yes, that seems to be the key to the puzzle somehow.

Well, the main thing is that I have verified that showMessageDialog() is
working fine; I just have to do some more research into file formats and/or
opening files correctly so that they can be displayed as icons.

Thanks again for your help, Roland!

Rhino
Roland - 06 Feb 2005 23:05 GMT
>>Works for me.
>>Windows 2000
[quoted text clipped - 18 lines]
> Of course, if I execute the decoding, I *never* get the icon to display so
> I'm further ahead following your suggestion ;-)
But it *did* work equally well with your URL-to-String decoding. I just
thought it was a bit long-winded, since there is the constructor which
takes a URL.

> Since I expect at least the occasional file name that includes embedded
> blanks, like "My About.gif", I think I need to search for a solution that
> works all of the time, rather than assuming the file  names won't contain
> embedded blanks.

Just tried it with a file containing space chars 'duke on swing.gif': no
problem here. Printing the url shows that the spaces correctly have been
URL escaped to %20:
file:/F:/eclipse/workspace/EclipseTry/bin/images/duke%20on%20swing.gif

Maybe previous versions of Java didn't behave well with these characters.

>>The only time I don't get an icon on the 3rd dialog, is when the image
>>is not a valid gif (I created a textfile boom.txt and renamed it to
[quoted text clipped - 7 lines]
> working fine; I just have to do some more research into file formats and/or
> opening files correctly so that they can be displayed as icons.
AFAIK, bmp, gif, jpeg and png are supported. Since gif and png support
transparent pixels, they are well suited for icons. [I used duke.gif
from j2sdk1.5.0\demo\jfc\Metalworks\HelpFiles].
Of course if the image contains errors, Java cannot display it. I've see
only 1 or 2 gif images that caused Java to fail (while IrfanView could
display them).

> Thanks again for your help, Roland!
Glad I could help.

> Rhino
Signature

Regards,

Roland de Ruiter
  ___      ___
 /__/ w_/ /__/
/  \ /_/ /  \

Rhino - 07 Feb 2005 00:18 GMT
> >>Works for me.
> >>Windows 2000
[quoted text clipped - 21 lines]
> thought it was a bit long-winded, since there is the constructor which
> takes a URL.

Interesting....

I suspect the difference in our experiences is because I am reading my image
files (GIFs,JPGs) from a jar file. When there is an embedded blank in the
name of the image file (as opposed to the name of the jar file), I get
problems. If the image file name has no embedded blank, everything works
fine.

> > Since I expect at least the occasional file name that includes embedded
> > blanks, like "My About.gif", I think I need to search for a solution that
[quoted text clipped - 7 lines]
>
> Maybe previous versions of Java didn't behave well with these characters.

I think the difference is that my gifs are in a jar and yours are not.
Somehow, that causes problems in my code.

> >>The only time I don't get an icon on the 3rd dialog, is when the image
> >>is not a valid gif (I created a textfile boom.txt and renamed it to
[quoted text clipped - 8 lines]
> > opening files correctly so that they can be displayed as icons.
> AFAIK, bmp, gif, jpeg and png are supported.

I thought it was just gif, jpg, and png but NOT bmp. Mind you, I haven't
tried a bmp yet - nor a png for that matter - but I think I saw something in
the API that said gif, jpg and png were the three acceptable formats.

>Since gif and png support
> transparent pixels, they are well suited for icons. [I used duke.gif
> from j2sdk1.5.0\demo\jfc\Metalworks\HelpFiles].
> Of course if the image contains errors, Java cannot display it. I've see
> only 1 or 2 gif images that caused Java to fail (while IrfanView could
> display them).

I know that my gif images are okay because I can view them in other
contexts.

Clearly, I'm going to have to do a bit of research on proper methods of
getting and reading image files from jars that have embedded blanks in their
names.

Rhino


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.