Two Laptop Bag*
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 Murach's Java Servlets and JSP this week in the Servlets 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

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.
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

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

Joined: Oct 27, 2005
Posts: 19649

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

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

Joined: Jul 08, 2003
Posts: 24183

Yes -- that was a typo.

[Jess in Action][AskingGoodQuestions]
It is sorta covered in the JavaRanch Style Guide.
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. :)?