Both of them are called with the constructor to class GifLoader. InitGUI() instantiates the Swing components and the URL and the Icon and addWindowProperties() sets the JFrame properties. } catch (Exception e) įrame.pack() // you don't know the size of the gif, so packįtDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
Why take the hassle of measuring things? Chill!įrame = new JFrame("Loader") // windows title is Loader, change Unless you know the size of the animated gif, it’s better to use pack() and let Swing do the work for you. Let’s just keep things simple and use generic exception handlers. To make sure you get the animated gif and not a static frame or image, you’ve to load it as a stream via URL object and some exception handling will need to be taken care of. Finally, set the Icon object to the JLabel. Then instantiate an Icon object with the URL. So what we’re going to do here is that we’re going to load the animated gif from the URL given earlier into an URL object. JLabel can have Icon objects set to them as Icons. We just need a JFrame, a JLabel and that’s it!įor testing whether our loader works or not we’ll use this animated gif : TotoroGif We’ll write a simple animated gif loader using Java Swing components. So how do you load them in a Java application? Or let’s say you want a gifloader that loads the gif of your choice?Īnimated Gif Loader !!! Ting Ting Ting!!!! Gifs are pretty good at expressing things in short – just like those tiny stories that float around on social networks. Especially after people realized picture comments ain’t enough for their social networking experience. Animated gifs are just all over the internet these days.