Hope it's not too late for this.
I managed to get an animated gif inside my JPanel this way:
private JPanel loadingPanel() { JPanel panel = new JPanel(); BoxLayout layoutMgr = new BoxLayout(panel, BoxLayout.PAGE_AXIS); panel.setLayout(layoutMgr); ClassLoader cldr = this.getClass().getClassLoader(); java.net.URL imageURL = cldr.getResource("img/spinner.gif"); ImageIcon imageIcon = new ImageIcon(imageURL); JLabel iconLabel = new JLabel(); iconLabel.setIcon(imageIcon); imageIcon.setImageObserver(iconLabel); JLabel label = new JLabel("Loading..."); panel.add(iconLabel); panel.add(label); return panel; }
Some points of this approach:
1. The image file is in the bank.
2. ImageIO.read () returns a BufferedImage that does not update ImageObserver.
3. Another alternative for finding images included in the jar file is to ask the Java class loader, the code that loads your program, to get the files. He knows where things are.
So by doing this, I was able to get my animated gif inside my JPanel, and it worked like a charm.
source share