meraj
meraj

Reputation: 243

How do i add a number images from a directory in a java swing form?

For my project I need to show a number of images from a file directory in a java swing form. The directory and hence the number of images and their path change on every execution. I am planning to show a fixed number of images say 5 in the form and provide a button to load subsequent images in the same place.The image filenames are sequential(like eg1.jpg,eg2.jpg, eg3.jpg...) I am using Netbeans 6.9.1 IDE

Upvotes: 1

Views: 1771

Answers (3)

Dhruv Gairola
Dhruv Gairola

Reputation: 9182

i have just the solution for you. i worked on a similar project and found this link extremely helpful for displaying the images. download, run and understand the "IconDemo" project. note that there are 2 separate threads in this project- the SwingWorker to load images (cos this is a resource intensive task) and the EventDispatchThread which concerns the GUI.

as for loading pictures from a directory, you can integrate the above project with a JFileChooser. here is my code below:

    //return file (image) names in the chosen directory
    public ArrayList<String> getFileNames() 
    {
        //widget to let users select a directory or file
        JFileChooser chooser = new JFileChooser();
        //holds all file (image) names in the chosen directory
        ArrayList<String> myArr = new ArrayList<String>();
        
        //only allow directory selection
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        //current directory is set
        chooser.setCurrentDirectory(new java.io.File("."));
        
        //pops up file chooser dialog, user chooses a directory
        int returnVal = chooser.showOpenDialog(null);
        
        //if the selected option was approved
        if(returnVal == JFileChooser.APPROVE_OPTION) 
        {
            //directory object
            File folder = chooser.getSelectedFile();
            //directory string
            directory = chooser.getSelectedFile() + "\\";
            //list files objects in the directory object
            listOfFiles = folder.listFiles();
            
            //put all the names of the file objects into myArr
            for (int i = 0; i < listOfFiles.length; i++) 
            {
                if (listOfFiles[i].isFile()) 
                {
                    myArr.add(directory+listOfFiles[i].getName());
                }//end inner if
            }//end for         
        }//end outer if
        //else no selection was made
        else 
        {
            System.out.println("No Selection ");
        }//end else
        
        return myArr;
    }//end method

comments on my code above:

this code uses the JFileChooser to allow you to select a directory and return an ArrayList of Strings of all the image files in that folder. You can later use these Strings to make ImageIcons by iterating through the ArrayList one by one, and restrict to only 5 strings for 5 images. If you have read my link provided above, creating ImageIcons should be a chinch. Last step is to integrate all of the above with a java swing form- which would be obvious by now, trivial, really. You can create a button to load more than 5 images. Good luck!

Upvotes: 1

user489041
user489041

Reputation: 28304

I image a JFrame that contains a JScrollPane. This JScrollPane contains a JPanel. The JPanel has a GridLayout for the number of pictures. Set the Viewport of the JSCrollPane to the JPanel. Add the pictures to the JPanels.


JFrame theFrame = new JFrame();
JPanel picPanel = new JPanel();
JScrollPane scoller = new JScrollPane(picPanel);
theFrame.add(scroller);
//set layout to number of pics
picPanel.setLayout(new GridLayout(numOfPics,1));
//add pics to pic panel

You can use ImageIO to read in images from a directory


BufferedImage newImage = ImageIO.read(theFile)

And then add it to a JLabel which can then be added to the JPanel


JLabel newImageLabel = new JLabel();
newImage.setIcon(new ImageIcon(newImage));
picPanel.add(newImageLabel);


Upvotes: 0

jzd
jzd

Reputation: 23629

  1. Parse the list of files in the directory.
  2. Loop through the list and display up to the first five.
  3. Add a button if more than 5 images exist.

There are several ways to add images to a container in Swing. One of the easiest is to just add it as an icon to a JLabel. I strongly recommend doing this by hand rather than using a GUI tool.

Upvotes: 2

Related Questions