*
The moose likes Beginning Java and the fly likes how do I make an array like this? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how do I make an array like this?" Watch "how do I make an array like this?" New topic
Author

how do I make an array like this?

colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

How could put all of the gif images in a directory into an array of ImageIcons? Would it be something like:

ImageIcon[] imageArray = ImageIcon["C:\pixs\*.gif"];

Everything I try comes up with a imcompatible type error.

Any help would be appreciated.


www.mormon.org
Robert Hill
Ranch Hand

Joined: Feb 24, 2006
Posts: 94
ImageIcon[] imageArray = ImageIcon["C:\pixs\*.gif"];


The array declaration is looking for an integer to use for its size, not a String.

Secondly, passing a String into an array element won't work.A String is definitely NOT an ImageIcon. So this will not work either

ImageIcon[] imageArray = ImageIcon[100];

...

imageArray[0] = "C:\pixs\*.gif"

Hint: Look up ImageIcon in the API docs.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

One more thing: In a Java string literal, the backslash is used as an escape character. To include a literal backslash in a string, you need to use \\, so it should be: "C:\\pixs\\*.gif"


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18


[ November 20, 2007: Message edited by: Rob Prime ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
colton peterson
Ranch Hand

Joined: Nov 18, 2007
Posts: 97

Thanks. One more thing, when he said:

icons[i] = new ImageIcon(files.getAbsolutePath());

he didn't specify what element in the array he was using. Can you do that? Wouldn't it be more like:

icons[i] = new ImageIcon(files[i].getAbsolutePath());
[ November 20, 2007: Message edited by: colton peterson ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Yes -- that was a typo.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how do I make an array like this?
 
Similar Threads
updating an image with a new color and or content
Mouse shift event coding - to trigger a predetermined sequence of images to a GUI
Animated GIF
Retrieving a ani-gif from serialized ImageIcon
JAVA ImageIcon and JButton resizing problem -- please go easy on me. :)?