-2

I want to load animated gif to jpanel so I read how to do it but when I run the program

i get this message:

java.net.MalformedURLException: no protocol: /Users/me/Documents/workspace/ManageTimeClock/Images/timeanim.gif

I checked my gif path and its correct!!

So what should i do?

my code:

URL url;
            try {
                url = new URL("/Users/me/Documents/workspace/ManageTimeClock/Images/timeanim.gif");
                Icon imgGif = new ImageIcon(url);
                JLabel lblGif = new JLabel();
                lblGif.setIcon(imgGif);
                lblGif.setBounds(100, 100, 400, 450);
                switchPanel.add(lblGif);
            } catch (MalformedURLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

i solved it thank to all anyway :)

Java Man
  • 1,854
  • 3
  • 21
  • 43
Dor Cohen
  • 117
  • 1
  • 4
  • 10
  • can you try adding file:// to the URI? – rogue-one Mar 06 '14 at 09:35
  • What part of the Javadocs for `URL` are confusing you? That isn't an URL because .. there's no protocol in it. – Brian Roach Mar 06 '14 at 09:35
  • yes..stil no help@mig-25foxbat – Dor Cohen Mar 06 '14 at 09:35
  • You can [answer your own question](http://meta.stackexchange.com/q/17463/163188). – trashgod Mar 06 '14 at 09:40
  • Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Mar 06 '14 at 09:54

2 Answers2

0

solved it with:

String path = "/Users/me/Documents/workspace/ManageTimeClock/Images/timeanim.gif";

                Icon imgGif = new ImageIcon(path);
                JLabel lblGif = new JLabel();
                lblGif.setIcon(imgGif);
                lblGif.setBounds(150,150, 455, 170);
                switchPanel.add(lblGif);
Dor Cohen
  • 117
  • 1
  • 4
  • 10
  • 1
    By the time of deployment, those resources will likely become an [tag:embedded-resource]. That being the case, the resource must be accessed by `URL` instead of `File`. See the [info page](http://stackoverflow.com/tags/embedded-resource/info) for the tag, for a way to form an `URL`. – Andrew Thompson Mar 06 '14 at 09:53
0
       Try This 
       import java.awt.*;
       import javax.swing.JFrame;
       import javax.swing.JPanel;
       import java.awt.BorderLayout;
       import javax.swing.JLabel;
       import javax.swing.ImageIcon;

class MainFrame extends JFrame {
   JPanel contentPane;
   JLabel imageLabel = new JLabel();
   JLabel headerLabel = new JLabel();

public MainFrame() {
    try {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        contentPane = (JPanel) getContentPane();
        contentPane.setLayout(new BorderLayout());
        setSize(new Dimension(400, 300));
        headerLabel.setFont(new java.awt.Font("Comic Sans MS", Font.BOLD, 16));
        headerLabel.setText("Animated Image in java");
        contentPane.add(headerLabel, java.awt.BorderLayout.NORTH);
        ImageIcon ii = new ImageIcon(this.getClass().getResource(
                "activity.gif"));
        imageLabel.setIcon(ii);
        contentPane.add(imageLabel, java.awt.BorderLayout.CENTER);
        // show it
        this.setLocationRelativeTo(null);
        this.setVisible(true);
     } 
    catch (Exception exception) {
        exception.printStackTrace();
    }
}

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

}

Here you can see gif image inside frame

Here you can see gif image inside frame

Benjamin
  • 2,257
  • 1
  • 15
  • 24