i would need same help, i don't understand why this is not working
what i'm trying to do is to make a small slideshow. i searched the web and i saw that this it should be done... my problem is that the JLabel that should contain an ImageIcon doesn't load at all and when i insert an image from folder it also doesn't load

i appreciate any help:D

public class Slide {
	
	public static JButton button;
	public static JLabel label;
	public static ImageIcon img1, img2, img3;
	public static JFrame frame;
	
	public static void main(String args[]) {
		
		button = new JButton("Click!");
		img1 = new ImageIcon("http://calindragan.files.wordpress.com/2010/06/pictura_muzica.jpg");
		label = new JLabel(img1);
		//label.setIcon(img1);
		//label.setSize(100, 100);
		frame = new JFrame("Slide");
		
		frame.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));
		JFrame.setDefaultLookAndFeelDecorated(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		frame.setSize(450, 420);
		frame.setVisible(true);	
		frame.add(label);
		//frame.add(new JLabel(img1));
		frame.add(button);
	}
}

Your problem is you are passing the wrong args to the ImageIcon constructor.
Read the API doc to see what kind of String value is valid and whether there should be another type of arg used.

Sorry Norm, it looks OK to me. There is a constructor that takes an Image, and ImageIcon is appropriate.

I was talking about the ImageIcon's constructor.

Norm: I'm so sorry. I didn't read yourt post properly. My bad,
J

i solved my problem... it's not the best way that can be done but i'm posting it because someone might use:)

if anyone has an idea of improvement let me know:)

public class Slide {

	public static JButton button;
	public static JLabel label;
	public static ImageIcon img1, img2, img3;
	public static JFrame frame;
	public static Integer a;
	public static URL url1, url2, url3;
	public static File file1, file2, file3;
	public static String display[];

	//procedure to choose a random
	public static String random() {
		//creating an array to choose from
		display = new String[3];
		display[0] = "url1";
		display[1] = "url2";
		display[2] = "url3";
		// chooses a number from 1 to 3
		a = (int) (Math.random() * 3);
		//chooses the "randomth" element from array
		String rnd = display[a];
		return rnd;
	}

	@SuppressWarnings("deprecation")
	public static void main(String args[]) {
		url1 = null;
		url2 = null;
		url3 = null;
		//creating a File instance to get an image from file
		file1 = new File(
				"/home/noon/workspace/eclipse/Applets/src/images/ima1.jpg");
		file2 = new File(
				"/home/noon/workspace/eclipse/Applets/src/images/ima2.jpg");
		file3 = new File(
				"/home/noon/workspace/eclipse/Applets/src/images/ima3.png");
		button = new JButton("Click!");

		try {
			// the URL takes the file from the given location
			url1 = file1.toURL();
			url2 = file2.toURL();
			url3 = file3.toURL();
		} catch (MalformedURLException e1) {

			e1.printStackTrace();
		}
		//ImageIcon takes the image from the given URL
		img1 = new ImageIcon(url1);
		img2 = new ImageIcon(url2);
		img3 = new ImageIcon(url3);
		//set the label with an image 
		label = new JLabel(img1);

		frame = new JFrame("Slide");
		frame.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));
		JFrame.setDefaultLookAndFeelDecorated(true);

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		frame.setSize(200, 220);
		frame.setVisible(true);
		frame.add(label);
		frame.add(button);

		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				//if the button if pressed...
				if (e.getSource() == button) {
					//apply the random() procedure
					String d = random();
					//Image myImg = Toolkit.getDefaultToolkit().getImage(d);
					System.out.println(d);
					//display the correct image in label
					if (d == "url1") {
						label.setIcon(img1);
					} else if (d == "url2") {
						label.setIcon(img2);
					}
					if (d == "url3") {
						label.setIcon(img3);
					}
				}
			}
		});
	}
}

If you use the image file in your own computer to create a ImageIcon instance, e.g. img1, the code in the first post you made will work well. You may simply replace the URL in line 11:
"http://calindragan.files.wordpress.com/2010/06/pictura_muzica.jpg"
by
"/home/noon/workspace/eclipse/Applets/src/images/ima1.jpg"
Try it.

it didn't work like that for me, i don't know why...
i posted the same program with the changes i made

I have modified your final code for the image files located in your own computer.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Slide1 {
	public static JButton button;
	public static JLabel label;
	public static ImageIcon img[]=new ImageIcon[3];
	public static JFrame frame;
	public static int count=0;
	public static void main(String args[]) {
		for (int i=0; i<3;i++)
img[i]=new ImageIcon("/home/noon/workspace/eclipse/Applets/src/images/ima" + (i+1) + "png");
		button = new JButton("Click!");
		label = new JLabel(img[1]);
		frame = new JFrame("Slide");
		frame.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20));
		frame.add(label);
		frame.add(button);
		JFrame.setDefaultLookAndFeelDecorated(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(750, 500);
		frame.setVisible(true);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			label.setIcon(img[(int)(Math.random()*3)]);			
			}
		});
	}
}

it didn't work like that for me, i don't know why...

The original code used a String that the method thought was to a local file, see the API doc. The String looked like a url but was not a URL object. The overridden method used the String for a local file. The String was not a URL object.

I am also noticed that if the image file stored in your computer is an animated gif, it will be shown like cartoon with its own thread (provided it is obtained in the above way). However if you obtain the image file via other method such as the method getImage(getCodeBase(),...) privided by Applet, an animated gif will have no animation (dead).

thank you a lot for the tip, i was going to work on it to make it more general, but because of the URL-s problems i got i gave up

i'm a newbie and i worked a few days to figure how to make this 'Slide'

I am sorry for the above post which is partially correct.
I should say: "I am also noticed that if the image file stored in your computer is an animated gif, it will be shown like cartoon with its own thread (provided it is obtained in the above way)." However if you obtain the image file via other method such as the method

try {
      File f = new File("Alberta.jpg");
      image = ImageIO.read(f);
    } catch (Exception e) {}

provided by javax.imageio.ImageIO
, an animated gif will have no animation (dead).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.