Need Help... Urgent..

Hi guys... I am trying implement a smileys list window.. When I load the images to a JList control there's a something weird thing happened. Some images load successfully but some don抰.. is there anything wrong on my coding?

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import java.util.*;

class smileysextends JFrame

{

Vector ms=new Vector();

ImageIcon img;

JList jl;

JScrollPane jsp;

public smileys()

{

setSize(250,200);

setTitle("Insert Smiley");

setDefaultCloseOperation(EXIT_ON_CLOSE);

Container c=getContentPane();

for(int x=1;x<54;x++)

{

if(x==3 || x==5 || x==22 || x==27 || x==30 || x==32 || x==33 || x==42 || x==44 || x==48 || x==50 || x==51)

{

continue;

}

else

{

img=new ImageIcon("c:/smilieys/" + x +".gif");

System.out.println("c:/smilieys/" + x +".gif");

ms.add(img);

}

}

jl=new JList(ms);

jl.updateUI();

jsp=new JScrollPane(jl);

c.add(jsp);

setVisible(true);

}

publicstaticvoid main(String arg[])

{

smileys sm=new smileys();

}

publicvoid actionExecuted(String name)

{

}

}

[2529 byte] By [asiridola] at [2007-11-26 16:12:09]
# 1

Hi

Here are the things I normally do when loading Images.

1) Make sure your file names are the same case. Some image editors saves the extensions of files to uppercase.

2) Make sure that your images is in the classpath.

3) Use the following to load images

img = new ImageIcon(smileys.class.getClassLoader().getResource("c:/smilieys/" + x + ".gif")).getImage()

I always use the Class Loader, that way you sure that it will search the correct paths for your images. This also works when you create jar files and include your images. Even with applets !!!!!! Trust me I'v spend months on getting to load images correctly. And as always the solution is a simple 'One Liner' :-)

CarelDuToita at 2007-7-8 22:34:44 > top of Java-index,Desktop,Core GUI APIs...
# 2

for(int x=1;x<54;x++)

{

if(x==3 || x==5 || x==22 || x==27 || x==30 || x==32 || x==33 || x==42 || x==44 || x==48 || x==50 || x==51)

{

continue;

}

else

{

img=new ImageIcon("c:/smilieys/" + x + ".gif");

System.out.println("c:/smilieys/" + x + ".gif");

ms.add(img);

}

}

WTF... If I ever saw that sort of code in an application I'd sit the author down for a chat :o)

And, as above, use the classloader. Though note that the code suggested won't work - you'll need to change your path string to a relative classpath value rather than an absolute filesystem value. Eg,

img = new ImageIcon(getClass().getResource("images/" + x + ".gif"));

itchyscratchya at 2007-7-8 22:34:44 > top of Java-index,Desktop,Core GUI APIs...
# 3

> for(int x=1;x<54;x++)

> {

> if(x==3 || x==5 || x==22 || x==27 || x==30 ||

> || x==32 || x==33 || x==42 || x==44 || x==48 || x==50

> || x==51)

> {

> continue;

> }

>

>

> WTF... If I ever saw that sort of code in an

> application I'd sit the author down for a chat :o)

>

That's only because you don't know enough about numerology :-P

3 - the first odd prime number

5 - the first safe prime number

22 - the number of paths between the sephiroth (in the Kabbalah)

27 - the first composite number not evenly divisible by any of its digits (in base 10)

30 - the first sphenic number

32 - the number of variations in Bach's Goldberg variations

33 - the smallest integer such that it and the next two integers all have the same number of divisors

42 - really, do I need to explain this one?

44 - Hank Aaron's jersey number

48 - the number of chromosomes apes have

50 - the smallest number that can be written as the sum of two squares in two distinct ways

51 - the sloping angle of the Great Pyramid's sides is 51 degrees and 51 minutes

Torgila at 2007-7-8 22:34:44 > top of Java-index,Desktop,Core GUI APIs...
# 4
oh guys.. jusst forget the if condition.... :) anyway thnx for all... I have sorted out the problem... :DMessage was edited by: asiridol
asiridola at 2007-7-8 22:34:44 > top of Java-index,Desktop,Core GUI APIs...