0

I'm creating a Guess Who game as an independent final project for my object-oriented university class and was running into an issue. One of the things I want my program to be able to do is let the user upload his/her own files from the computer to be used in the guess who game. Basically, the user clicks a JRadioButton and then the FileChooser box will open so he/she can navigate to the folder with the files. I realize that you can use the setMultiSelectionEnabled(true) command to make it so that you can select multiple files, but is there a way that I can restrict the selection to only 25 images (the size of my game-board)? Is there an easier way of doing this? Should I just make it so that the user can only select folders filled with images?

The reason I want the specific files is because I want to load the images into an ImageIcon array and the names of the files (before the extensions), into an array as well.

Here is the code I have so far:

private class fileSelector implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            JFileChooser files = new JFileChooser(); //creates a new filechooser
            files.setCurrentDirectory(new File(System.getProperty("user.home"))); //starts the filechooser at the home directory
            FileNameExtensionFilter filter = new FileNameExtensionFilter("*.Images", "jpg", "png", "gif"); //only allows files with these extensions to be used
            files.addChoosableFileFilter(filter); //adds the filter


            files.setMultiSelectionEnabled(true); //makes it so you can select multiple files!
            files.showOpenDialog(null);
       }

    }


Any help would be great! Thanks!

2 Answers2

0

sadly their is no way to do this cause this is ComponentUI related !

@trashgod made great examples here

also you can make an FileFilter like this

public class ImagesFilter extends FileFilter {

        @Override
        public boolean accept(File f) {
            if (f.isDirectory()) {
                File[] list = f.listFiles();
                if (list.length == 25) {
                    boolean ret = true;
                    for (File file : list) {
                        ret = ret && isMyImageType(file);
                    }
                    return ret;
                }
            }
            return false;
        }

        @Override
        public String getDescription() {
            //descripe it .
            return "";
        }

    }

and then later in the JFileChooser.getIcon(File f) override it to get a special icon that suits your project with the same class like :-

private final ImagesFilter filter = new ImagesFilter();
            @Override
            public Icon getIcon(File f) {
                if (filter.accept(f))
                {
                    //return your icon
                }
                return super.getIcon(f); //To change body of generated methods, choose 
            }
OverLoadedBurden
  • 606
  • 1
  • 5
  • 14
0

When you want to do something when a component changes (an event takes place), use a PropertyListener. Every time a user changes his selection an event is taking place. You can add a property listener to your file chooser and check if he has selected more files than you want.

Take a look at this example (max files 2):

    JFileChooser files = new JFileChooser(); // creates a new filechooser
    files.setCurrentDirectory(new File(System.getProperty("user.home"))); // starts the filechooser at the home
                                                                            // directory
    FileNameExtensionFilter filter = new FileNameExtensionFilter("*.Images", "jpg", "png", "gif"); // only allows
                                                                                                    // be used
    files.addChoosableFileFilter(filter); // adds the filter

    files.setMultiSelectionEnabled(true); // makes it so you can select multiple files!
    files.addPropertyChangeListener(e -> {
        File[] selectedFiles = files.getSelectedFiles();
        if (selectedFiles.length > 2) {
            File[] selectedFilesNew = new File[2];
            // Select the first 2
            for (int i = 0; i < selectedFilesNew.length; i++) {
                selectedFilesNew[i] = selectedFiles[i];
            }
            files.setSelectedFiles(selectedFilesNew);
            JOptionPane.showMessageDialog(files, "Only 2 selected files allowed.", "File chooser",
                    JOptionPane.ERROR_MESSAGE);
        }
    });
    files.showOpenDialog(null);

Rembember though, that this is a file count restriction and not a folder count restriction.

George Z.
  • 6,643
  • 4
  • 27
  • 47