Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how do I make an array like this?

 
colton peterson
Ranch Hand
Posts: 97
Linux Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ November 20, 2007: Message edited by: Rob Prime ]
 
colton peterson
Ranch Hand
Posts: 97
Linux Python VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes -- that was a typo.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic