Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8668393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:12:11+00:00 2026-06-12T18:12:11+00:00

Icon icon = new ImageIcon(getClass().getResource( /img/icon.gif ) ); aButton = new JButton(Its a button,

  • 0
Icon icon = new ImageIcon(getClass().getResource( "/img/icon.gif" ) );
aButton = new JButton("Its a button", icon);

Is there some kind of method that can stop an animated from playing?
I was thinking of assigning a static jpg of the gif, then when I hover, assign the animated gif, but I don’t think there is an event for taking off mouse in MouseMotionListener so I can load back the static jpg.

The gif loops in the button, however, if I hover over it, it disappears.

How can I make the gif static if my mouse cursor is not on the button?

If I use MouseMotionListener, does it fire an event if I take off my mouse?

@Override
public void mouseMoved(MouseEvent e) {
//play the gif
//if I take mouse off, call some method to stop playing animated gif
}

@Override
public void mouseDragged(MouseEvent e) {
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:12:13+00:00Added an answer on June 12, 2026 at 6:12 pm

    See:

    • setIcon(Icon)
    • setDisabledIcon(Icon)
    • setPressedIcon(Icon)
    • setRolloverIcon(Icon)
    • setSelectedIcon(Icon)

    No need for setting an explicit mouse listener, the changeover happens automatically.

    E.G. In this example I did not add a MediaTracker so popped the image into a label to allow for load time. The end user is the ImageObserver (wait till you see it spin before dismissing the first dialog).

    import java.awt.*;
    import java.awt.image.BufferedImage;
    import java.net.URL;
    import javax.swing.*;
    
    public class ImageSwapOnButton {
    
        public static void main( String[] args ) throws Exception {
            URL url = new URL("http://1point1c.org/gif/thum/plnttm.gif");
    
            Image image = Toolkit.getDefaultToolkit().createImage(url);
            ImageIcon spinIcon = new ImageIcon(image);
            JOptionPane.showMessageDialog(null, new JLabel(spinIcon));
    
            // create a static version of this icon
            BufferedImage bi = new BufferedImage(150,150,BufferedImage.TYPE_INT_ARGB);
            Graphics g = bi.getGraphics();
            g.drawImage(image,0,0,null);
            g.dispose();
            ImageIcon staticIcon = new ImageIcon(bi);
    
            JButton button = new JButton(staticIcon);
            button.setRolloverIcon(spinIcon);
            JOptionPane.showMessageDialog(null, button);
        }
    }
    

    Also, don’t make the static image as JPEG. A JPEG is lossy and does not support transparency. Either use a single frame GIF or a PNG.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

BufferedImage img = null; ImageIcon icon = null; try { img = ImageIO.read(new File(resources/
I have some code for a JButton: solutionButton = new JButton(Solution); solutionButton.setBorderPainted( false); solutionButton.setContentAreaFilled(
public class AthleteManager { private static Icon anIcon = new ImageIcon(); private static int
I have several JLabels, each holding an ImageIcon like this: ImageIcon icon = new
When hovering over each icon in http://www.ariasblog.ca/ there is a rotating effect before the
How can I apply a GIF image to my AWT Button ? AWT :
I use Netbeans IDE, some where in the code I included this ImageIcon accept_icon
I have a JComboBox that its renderer is a JLabel with an Icon for
Is it possible to use an animated .gif image as the icon of a
I have tried several methods to add an Icon to a JFrame. Every method

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.