load images from directory

all images from directyory to array or image icons

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 12144 Views - Last Post: 11 April 2010 - 12:34 PM Rate Topic: -----

#1 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 444
  • Joined: 15-May 08

load images from directory

Posted 10 April 2010 - 09:51 AM

Is it possible to scan an directory for a file extension say ".png" and then load each image into its own JButton using image icons?

If it is possible can someone direct me to a page that explains how to accomplish this?

I have no idea what to search to find what I'm looking for.

Thank you.
Is This A Good Question/Topic? 0
  • +

Replies To: load images from directory

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4337
  • View blog
  • Posts: 12,137
  • Joined: 18-April 07

Re: load images from directory

Posted 10 April 2010 - 10:20 AM

To get a list of files you can use the File class and its method listFiles(). There is a listFiles method overload which takes a FileFilter object that can be used to filter the types of files it will return. I have included the documentation to this method below...

File Class - listFiles() method

Once you have a list of files you can use the ImageIcon class to then construct an ImageIcon object (which implements the Icon interface so it will work with your JButtons) and then when you create the JButton just use this ImageIcon class instance.

JButton myButton = new JButton(new ImageIcon("c:\\mynewimage.png"));



So that should get you started. If you have further troubles, then show us your code in code tags please. Thanks!


"At DIC we be ImageIcon creating code ninjas... in the coding world, DIC is full of legendary coding icons!" :snap:
Was This Post Helpful? 2
  • +
  • -

#3 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 444
  • Joined: 15-May 08

Re: load images from directory

Posted 10 April 2010 - 11:13 AM

I don't know how to use the java docs. So i don't know how to make a File object or how to use the information you gave me I looked around on how to use the Java docs but they go off into what he/she exactly means.

Can anyone explain how I would use the Java docs to create code?

Thank you
Was This Post Helpful? 0
  • +
  • -

#4 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 444
  • Joined: 15-May 08

Re: load images from directory

Posted 10 April 2010 - 12:20 PM

I cant figure this out.

This is all i got so far and its nothing...

import java.io.File;

public class directoryreader
{
	public static void main(String args[])
	{
		String s = "";
		File f = new File(s);
		f.listFiles();
	}
}


the java docs don't tell me how to use listfiles. and file filter just checks if path's exist.
Was This Post Helpful? 0
  • +
  • -

#5 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: load images from directory

Posted 10 April 2010 - 12:37 PM

The listFiles method returns an array of File Objects.

You could then loop through the returned array, performing whatever actions you want on the files.

For example, if you wanted to print out all the files in a directory:
for(File a : f.listFiles()){
    System.out.println(f.getName());
}


                String s = "";
                File f = new File(s);
In order to call listFiles() however, your File Object that you're invoking it on must be a directory.
Was This Post Helpful? 0
  • +
  • -

#6 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 444
  • Joined: 15-May 08

Re: load images from directory

Posted 10 April 2010 - 12:44 PM

Quote

                String s = "";
                File f = new File(s);
In order to call listFiles() however, your File Object that you're invoking it on must be a directory.


how do i make my File f a directory?

String s = "c:\images\";
File f = new File(s);


cant do that because i get illegal escape character and unclosed string literal

This post has been edited by giuseppe105: 10 April 2010 - 12:46 PM

Was This Post Helpful? 0
  • +
  • -

#7 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: load images from directory

Posted 10 April 2010 - 12:49 PM

Well if you wanted to do this:
String s = "c:\images\"


You would need to escape the \'s, like so:
String s = "c:\\images\\"


You should also perform some tests to make sure the input is a directory:
if(!f.isDirectory()){ //check if it's a folder
   System.out.println("Not a directory!");
}
   if(!f.exists()){ //check if it exists
      System.out.println("File doesn't exist!");
   }


This post has been edited by erik.price: 10 April 2010 - 12:50 PM

Was This Post Helpful? 0
  • +
  • -

#8 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 444
  • Joined: 15-May 08

Re: load images from directory

Posted 10 April 2010 - 01:01 PM

ok i managed to make my file a directory by makeing

String s = "c:/"

but the loop only returns the last word in my directory. as many times as there are files or directory's in that directory.

This post has been edited by giuseppe105: 10 April 2010 - 01:04 PM

Was This Post Helpful? 0
  • +
  • -

#9 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: load images from directory

Posted 10 April 2010 - 01:10 PM

Could you post the code you're using now?

I just realized I made a typo in my earlier post.

This:
for(File a : f.listFiles()){
    System.out.println(f.getName());
}


Should be this:
for(File a : f.listFiles()){
    System.out.println(a.getName());
}

Was This Post Helpful? 0
  • +
  • -

#10 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 444
  • Joined: 15-May 08

Re: load images from directory

Posted 10 April 2010 - 01:25 PM

Yea that was the problem it works now. I also managed to get it working a different way by copying some code form this link.

This is the code that I got. not with your loop.

import java.io.File;

public class directoryreader
{
	public static void main(String args[])
	{
		String s = "C:\\Documents and Settings\\user\\My Documents\\Downloads\\";
		File f = new File(s);
		
		Boolean b;
		b = f.isDirectory();
		System.out.println(B)/>;
		
		File[] fa = f.listFiles();
		
		for (int i=0; i< fa.length; i++) {
        	System.out.println(fa[i]);
        }
	}
}


Thank you this half solves this question but you had to tell me how to do this could you tell me how to learn to accomplish what I want without having to ask?

This post has been edited by giuseppe105: 10 April 2010 - 01:27 PM

Was This Post Helpful? 0
  • +
  • -

#11 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: load images from directory

Posted 10 April 2010 - 01:34 PM

Alright, glad you got it working :)

And if you're working with a class that you're unfamiliar with, or need some clarification on, always check out the Javadoc. In this case, you would go here. I would suggest first reading the few paragraphs explaining the usage of it, then checkout the constructor and method summaries.

If you know what you're looking for, but don't exactly know the method name, do a quick Ctrl-F and search for a key word, like "Directory", or "List", and look through the matching method summaries to see which fits your needs. If the Javadoc fails you initially, try to Google a similar snippet of code, see which methods/classes it uses, and then try looking through the appropriate Javadocs again :)
Was This Post Helpful? 1
  • +
  • -

#12 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 444
  • Joined: 15-May 08

Re: load images from directory

Posted 10 April 2010 - 01:57 PM

Thank you i will try attempting the file filter in the way you just explained.
Was This Post Helpful? 0
  • +
  • -

#13 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 444
  • Joined: 15-May 08

Re: load images from directory

Posted 10 April 2010 - 04:00 PM

I managed to get a file filter working but i just edited more code i found. the documentation on the file filter wasn't enough for me to understand what to do with it.

this is my filter
import java.io.File;

public class PngFilter extends javax.swing.filechooser.FileFilter
{
    public boolean accept(File f) 
    {
        return f.getName().toLowerCase().endsWith(".png");
    }
    
    public String getDescription() 
    {
        return "PNG files";
    }
}

Was This Post Helpful? 0
  • +
  • -

#14 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: load images from directory

Posted 10 April 2010 - 07:56 PM

You should check out this tutorial on using the docs then :) http://www.dreaminco...16518-java-api/


Alright, so from the API, you see that FileFilter is an interface, meaning you'll have to override all the methods. (luckily, there's only one -- accept)

So, let's look at the method summary:

Quote

boolean accept(File pathname)
Tests whether or not the specified abstract pathname should be included in a pathname list.


So, we know we have to take a File Object, and then determine whether or not to use it, based on the properties of the file

That's about it for this method, so all you have to do now is think up an implementation to fit your needs. In your case, you checked for .png files.
Was This Post Helpful? 0
  • +
  • -

#15 giuseppe105  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 444
  • Joined: 15-May 08

Re: load images from directory

Posted 11 April 2010 - 09:34 AM

Makes sence same as what i did above only instead of using the swing file filter it uses the IO one.

Iv run into another problem in my project.

When I need to grab the directory from the list it gives it to me in windows form. like "C:\"
this returns an error because it things its an escape command.

Is there a way to make it return like this "C:/" or do I have to use a replace method to scan every string that i grab and replace the "\" with a "/"

This post has been edited by giuseppe105: 11 April 2010 - 09:58 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2