1 2 Previous Next 23 Replies Latest reply on Oct 11, 2001 1:20 AM by 807592

    ImageIcon problem

    807592
      Well... I've finally been asked to use an image on a button instead of a label. Going back to a text I used early on (Sam's Teach Yourself Java 2 in 21 Days) I found an example that I was never able to get to work. It takes a small .gif file to be used as a button icon. The code for this follows:

      ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      import javax.swing.*;

      public class Icons extends JFrame {
      JButton[] buttons = new JButton[24];

      /** Creates new Icons */
      public Icons() {
      super("Icons");
      setSize(335, 318);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      JPanel pane = new JPanel();
      ImageIcon icon = new ImageIcon("3dman.gif");
      for (int i = 0; i < 24; i++) {
      buttons[i] = new JButton(icon);
      pane.add(buttons);
      }
      setContentPane(pane);
      show();
      }

      public static void main(String[] args) {
      Icons ike = new Icons();
      }
      }
      ~~~~~~~~~~~~~~~~~~~~~~~~~~

      The buttons display just fine, but very small, and without the icon. The .gif file is small enough for this purpose, so I'm out of ideas. I'll be glad to attach the file to anyone through email if need be.

      Has anyone run into this before?
      Thanks all

      p.s. Has anyone ever figured out why, when you paste code into the entry window for this forum, it is spaced correctly, but when it makes it to the forum all the spacing is gone?
        • 1. Re: ImageIcon problem
          807592
          I'm always a little skeptical about the ImageIcon(String) method. I'm not sure what it does about image loading. What I always use is along these lines:
          Image img = Toolkit.getDefaultToolkit().createImage("blah.gif");
          MediaTracker tracker = new MediaTracker(this);
          tracker.addImage(img, 0);
          tracker.waitForAll();
          ImageIcon icon = new ImageIcon(img);
          You'll need to catch some exceptions as well. One issue is if it can't find the gif, it won't throw an exception, you have to ask the MediaTracker whether there was an error after you're done waiting for the images.

          This sort of code should probably go into a separate method, too, so you can use it from everywhere.

          The forum FAQ answers your second question. Put the text [ code ] ... [ /code ] around your code blocks (without the spaces inside the [ ]), and it will automatically format it for you.
          • 2. Re: ImageIcon problem
            807592
            ImageIcon already has a MediaTracker no need to add it.
            The problem with "denison's" code is that buttons is used incorrectly in the for loop. buttons is an array of buttons. Just refer to a single button for each pass thru the loop. Use button[j] instead of buttons in loop.
            I know that it is i not j but i can't it to display because it gets mistaken for the special token for italic.
            How could i enter it correctly?
            • 3. Re: ImageIcon problem
              807592
              Yeah, I know the ImageIcon class has a MediaTracker in it, but I've often had problems with the image not being loaded by the time the icon is displayed (which were always fixed by loading the image outside of the ImageIcon class.)

              You can enter [ i ] inside a [ code ] block and it gets converted to < i >, which is readable.
              • 4. Re: ImageIcon problem
                807592
                The problem with "denison's" code is that buttons is used incorrectly in the for loop.
                buttons is an array of buttons. Just refer to a single button for each pass thru the loop.
                Use button[j] instead of buttons in loop.
                OK... that makes sense in this case... it's referring to an array but not using any of the indices... instead the line should read:

                JButton btn = new JButton(icon);
                pane.add(btn);

                or something to that effect? I'll have to try that when I can get back to this appl.
                Thanks again.
                • 5. Re: ImageIcon problem
                  807592
                  Please try,
                  for (int j = 0; i < 24; j++) {
                  buttons[j] = new JButton(icon);
                  pane.add(buttons[j]);
                  }
                  Or use i instead of j.
                  • 6. Re: ImageIcon problem
                    807592
                    OK... I realized, after reading this forums FAQ (thanks for the reminder, trejkaz ) that my array references were being dropped because of using the "i"... so the last suggestion was what was actually in the code. I replaced the "i" with "j" to eliminate this.

                    I attempted the suggestion using the MediaTracker... but now I'm getting the following message:

                    myproject/Java21Work/Day08/Icons.java [29:1] No method found matching waitForALL()
                    tracker.waitForALL();
                    ^

                    I also tried using: tracker.waitForID(0); but still get the "No method found matching...".
                    I've begun digging in the docs... but seeing as the folks on this forum are so responsive I thought I'd repost with my changes.

                    The new code follows:
                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~
                    import javax.swing.*;
                    import java.awt.*;
                    public class Icons extends JFrame {
                        JButton[] buttons = new JButton[24];
                    
                        /** Creates new Icons */
                        public Icons() {
                            super("Icons");
                            setSize(335, 318);
                            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                            JPanel pane = new JPanel();
                            // ImageIcon icon = new ImageIcon("3dman.gif");
                            Image img = Toolkit.getDefaultToolkit().createImage("3dman.gif");
                            MediaTracker tracker = new MediaTracker(this);
                            tracker.addImage(img, 0);
                            tracker.waitForALL();  // this is the method call giving the compiler error
                            ImageIcon icon = new ImageIcon(img);
                            
                            for (int j = 0; j < 24; j++) {
                                buttons[j] = new JButton(icon);
                                pane.add(buttons[j]);
                            }
                            setContentPane(pane);
                            show();
                        }
                        
                        public static void main(String[] args) {
                            Icons ike = new Icons();
                        }
                    }
                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~
                    Thanks again!
                    • 7. Re: ImageIcon problem
                      807592
                      Java is case sensitive. ;-)
                      • 8. Re: ImageIcon problem
                        807592
                        Your orginal code works for my machine.
                        • 9. Re: ImageIcon problem
                          807592
                          Hey trejkaz... indeed, Java is case sensitive... (it's always the little things, huh?) however, none of the combinations worked. That was until I read ALL of the compiler error messages... namely the one that said that this was an Exception... NOT a missing method. DOH!!!!
                          ~~~~~~~~~~~~~~~~~~~~~~~~~~
                          Image img = Toolkit.getDefaultToolkit().createImage("3dman.gif");
                          MediaTracker tracker = new MediaTracker(this);
                          tracker.addImage(img, 0);
                          try {
                               tracker.waitForID(0);
                               } catch (InterruptedException e) {}
                          ImageIcon icon = new ImageIcon(img);
                          ~~~~~~~~~~~~~~~~~~~~~~~~~~
                          Seems that the both:
                          tracker.waitForAll();
                          tracker.waitForID(0);
                          must be used in a Try/Catch block.

                          The extra code to implement MediaTracker doesn't seem to make a difference... it still doesn't load the icon into the button.

                          I think I can elimnate any Classpath problems... have no troubles with compiling or running applications or applets. Here is my system setup:
                          Windows ME
                          Forte4J (CE2.0) for my work... that hasn't been an issue for anything else.
                          JDK1.3.1_01

                          Any other ideas? Thanks, once again, for the suggestions.
                          • 10. Re: ImageIcon problem
                            807592
                            Interesting w234... this is my system setup:

                            Windows ME
                            Forte4J (CE2.0) for my work... that hasn't been an issue for anything else.
                            JDK1.3.1_01

                            What are you using? Oh... I forgot to mention that I run this from both the Command line AND through Forte... same result.

                            Thanks all!
                            • 11. Re: ImageIcon problem
                              807592
                              This is your orginal code that works on my machine.
                              import javax.swing.*;
                              public class Icons extends JFrame {
                              JButton[] buttons = new JButton[24];
                              /** Creates new Icons */
                              public Icons() {
                              super("Icons");
                              setSize(335, 318);
                              setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                              JPanel pane = new JPanel();
                              ImageIcon icon = new ImageIcon("3dman.gif");
                              for (int j = 0; j < 24; j++) {
                              buttons[j] = new JButton(icon);
                              pane.add(buttons[j]);
                              }
                              setContentPane(pane);
                              show();
                              }
                              public static void main(String[] args) {
                              Icons ike = new Icons();
                              }
                              }
                              Check to see if you are typing the image file name exactly.
                              Also you should put it in the same directory as the class file or type the path to it( see the description of the constructor for ImageIcon).
                              • 12. Re: ImageIcon problem
                                807592
                                Not necessarily the same directory as the path. This is actually yet another possible cause of the problem; it tries to load the image file from the path the application was started from, which isn't necessarily anywhere in the classpath.
                                • 13. Re: ImageIcon problem
                                  807592
                                  I haven't read all the responses, but if you still have that problem, I have 2 suggestions.
                                  1. put the whole path in the quotation ex: "c:\\project\\icon\\3dman.gif"
                                  2. make sure the file is in the right folder, if you use JBuilder, put it in the same folder with the project file. To find which folder is the right one, you can copy the image files to all the folders you think is possible;after it works, delete it one by one, then you'll find the right folder.

                                  number 1 is not so good if you plan to move the location of your source files. and number 2, I had to double click on the project file, instead opening it from JBuilder, to get the project recognize the path.

                                  kindo
                                  • 14. Re: ImageIcon problem
                                    807592
                                    Yeah, JBuilder can get a bit confused about user paths. The easy way to tell Java where it's being run from is to put -Duser.dir=... in the VM options.
                                    1 2 Previous Next