Is it not possible to have an animated .gif image as cursor?

Code fragment:

      Image image;
      JFrame mainframe;
      Cursor busyCursor = Toolkit.getDefaultToolkit().createCustomCursor(image, new Point(0,0), "PointedBusy");
      mainframe.setCursor(busyCursor);

When I load 'image' from any .gif  it works as a charm.
When I load an animated .gif the program hangs in the 'Toolkit' line.

;JOOP!
LVL 24
sciuriwareAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
I'd doubt it
CEHJCommented:
Where and how do you load your image?
sciuriwareAuthor Commented:
I load my .gif's from resource (.jar)

I checked again: it seems that I can't load an animated .gif, even into a JLabel.
Non-animated .gif's from the same directory give no problem.

If the animation would not be a problem, what would be the right way to load?

;JOOP!
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

sciuriwareAuthor Commented:
P.S.: my loading method creates an ImageIcon as intermediate.

;JOOP!
CEHJCommented:
>>If the animation would not be a problem, what would be the right way to load?

My concern was that the image may not be fully loaded on your cursor call
sciuriwareAuthor Commented:
That's not the problem, the following code snipped automatically waits for load completion:

         IconImage i = Toolkit.getDefaultToolkit().getImage(classInfo.getResource(filename));

;JOOP!
sciuriwareAuthor Commented:
Let put it this way: can you put an animated .gif into a JLabel or JButton?

;JOOP!
CEHJCommented:
I don't *think* so, but there's one way to find out ;-)
JavatmCommented:
> Let put it this way: can you put an animated .gif into a JLabel or JButton?

Yes and I've done it before ;)
JavatmCommented:
Or you can create a seperate class that defines what you want :
http://www.swingwiki.org/howto:animated_label
Mick BarryJava DeveloperCommented:
> can you put an animated .gif into a JLabel or JButton?

sure

heres an example of using them in a table

http://www.objects.com.au/java/examples/swing/AnimatedTableCell.do
sciuriwareAuthor Commented:
Thank you all for these examples, but I thought it was as simple as:

   JLabel x = new JLabel(Image); // Where Image is loaded from an animated gif picture.

;JOOP!
sciuriwareAuthor Commented:
new development: I sucessfully put a busy cursor on my JFrame:

    JFrame mainframe;

    mainframe.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));

At least: that works, but: this cursor doesn't show on a textarea inside the frame,
but it does show over buttons!
Is this a new problem or the same?

;JOOP!
Mick BarryJava DeveloperCommented:
> Thank you all for these examples, but I thought it was as simple as:

it can be, depends what you're doing with them.
sciuriwareAuthor Commented:
I verified the following:

<<< AnimatedGif.java >>>

import javax.swing.*;

public class AnimatedGif extends JFrame
{
  public AnimatedGif()
  {
    JLabel label = new JLabel(new ImageIcon("SomeAnimatedGif.gif"));
    getContentPane().add(label);
    pack();
    setVisible(true);
  }


  public static void main(String[] args)
  {
    new AnimatedGif();
  }
}

Great! Now remains: 1) animated (custom) cursor, 2) cursor on the entire JFrame.

;JOOP!
sciuriwareAuthor Commented:
Finally, try to run this (animated gif ex libris, but you can catch one from any advertisement):

public class Bugs
{
   /**
    * Program entrypoint.
    *
    * @param argv program command line vector.
    */
   public static void main(String[] argv)
   {
      JFrame mainFrame = new JFrame("Test cursors");
      ImageIcon ii;
      JLabel label = new JLabel(ii = new ImageIcon("/tmp/Animation.gif"));
      Point p = new Point(0,0);
      Toolkit tk = Toolkit.getDefaultToolkit();
      Image i = ii.getImage();
      Cursor ani = tk.createCustomCursor(i, p, "Mine");      
      mainFrame.setSize(800, 600);
      mainFrame.getContentPane().add(label);
      mainFrame.setVisible(true);
     
      while(true)
      {
         try
         {
            Thread.sleep(2000);
         }
         catch(InterruptedException e){ /* Pro Forma */ }
         mainFrame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
         
         try
         {
            Thread.sleep(2000);
         }
         catch(InterruptedException e){ /* Pro Forma */ }
         mainFrame.setCursor(ani);
      }
   }
}

The picture in the centre (JLabel) is animated.
The WAIT_CURSOR is animated.
The alternating copy of the gif shows but is NOT animated.
?????????????
;JOOP!
sciuriwareAuthor Commented:
I fear I have to delete this question as the main theme is not solved:

why does an animated gif loose its animation in a custom cursor? Is it a JAVA bug?

;JOOP!
Mick BarryJava DeveloperCommented:
it doesn't necessarily lose its animation, its to do with what createCustomCursor does with the image supplied to it.
it may just use the 1st frame for example.
Mick BarryJava DeveloperCommented:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sciuriwareAuthor Commented:
Ahhhhh finally the answer!

;JOOP!
Mick BarryJava DeveloperCommented:
:)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.