Questo esempio preleva i nomi dei file da una cartella utilizzando un array
package images_Show;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.List;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Images_Show extends JFrame{
Image img;
public Images_Show() throws IOException
{
setTitle("Image retrieved");
setSize(500, 500);
Container pane = getContentPane();
JLabel Photo;
pane.setLayout(new GridLayout(0,4)); // righe,colonne
String filepath ="F:/Download/B";
// String filepath ="F:/Documenti/Immagini";
File aDirectory = new File(filepath);
String[] filesInDir = aDirectory.list();
for (String file : filesInDir){
if(file.toLowerCase().endsWith(".jpg")){
img = ImageIO.read(new File(filepath+"/"+file));
img = img.getScaledInstance(300,200,Image.SCALE_SMOOTH);
ImageIcon icon =new ImageIcon(img);
Photo = new JLabel(icon) ;
pane.add(Photo) ;
this.pack();
this.setVisible(true);
}
}
}
public static void main(String args[]) throws IOException {
Images_Show frame = new Images_Show();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
}
}