0

How to load multiple files in Swing?

I have a single file upload button, and then I save the file in byte form, how to change the code so I can upload multiple files. I've tried many times but I have trouble .. I hope someone can help me

byte[] FileSurat;
String FilenameSurat;
File  imageSurat;

private void botton10ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    if (idPemohon == 0 && idRencanaTapak == 0){
        JOptionPane.showMessageDialog(this, "Pilih Data Yang akan di Upload");
    }else{

        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new File(System.getProperty("user.home")));
        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.addChoosableFileFilter(new FileNameExtensionFilter("Images", "jpg", "png", "gif", "bmp"));
        chooser.setAcceptAllFileFilterUsed(true);

        int i = chooser.showOpenDialog(this);
        if (i == JFileChooser.APPROVE_OPTION) {
             if(labelSurat != null){

                FileSurat = null;
                FilenameSurat = null;
                jPanel2.remove(labelSurat);
                jPanel2.validate();
                jPanel2.repaint();
            }
            try {
                imageSurat = chooser.getSelectedFile();
                String filename = chooser.getSelectedFile().getName();
                String extension = "";

                int y = filename.lastIndexOf('.');
                if (y > 0) {
                    extension = filename.substring(y+1);
                }

                String xx = chooser.getTypeDescription(imageSurat);
                if("JPEG image".equals(xx) || "PNG image".equals(xx) || "JPG image".equals(xx) || "GIF image".equals(xx) || "BMP image".equals(xx)){
                    BufferedImage originalImage = ImageIO.read(imageSurat);
                    if(originalImage == null){
                        JOptionPane.showMessageDialog(rootPane, "Format file corrupt");
                    }else{
                        int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
                        BufferedImage resizeImageJpg = resizeImage(originalImage, type);
                        photoSurat = new ImageIcon(toImage(resizeImageJpg));
                    }
                }else{
                    ico = new File(getClass().getResource("/images/no-image.png").getFile());
                    BufferedImage originalImage = ImageIO.read(ico);
                    int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();

                    BufferedImage resizeImageJpg = resizeImage(originalImage, type);
                    photoSurat = new ImageIcon(toImage(resizeImageJpg));
                }

                FilenameSurat = extension;
                FileSurat = new byte[(int) imageSurat.length()];
                FileInputStream fileInputStream = new FileInputStream(imageSurat);
                fileInputStream.read(FileSurat);

                //jPanel2.removeAll();


                labelSurat = new JLabel("", photoSurat, JLabel.RIGHT);
                jPanel2.add(labelSurat);

                ico = null;
                repaint();
                chooser.setCurrentDirectory(imageSurat);
            } catch (IOException ex) {
                Logger.getLogger(formUtama.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }
}    
mKorbel
  • 109,525
  • 20
  • 134
  • 319
mayus
  • 53
  • 2
  • 11
  • *"I've tried many times"* What have you tired, specifically? *"but I have trouble"* What trouble? Always copy/paste error & exception output. – Andrew Thompson Sep 24 '13 at 08:21
  • Please learn common [Java naming conventions](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307) (specifically the case used for the names) for class, method & attribute names & use them consistently. – Andrew Thompson Sep 24 '13 at 08:23
  • BTW - are you *sure* you mean *[tag:upload]* (which that code does not do), as opposed to *load & display* (which it does). – Andrew Thompson Sep 24 '13 at 08:28

2 Answers2

2

You switch the JFileChooser to allow multiple selection, and then you use the JFileChooser#getSelectedFiles method to obtain an array of Files instead of a single one.

Loop over the array using the same code you have and you're done

Robin
  • 36,233
  • 5
  • 47
  • 99
2

See the Many images section of this answer for two ways (of many) to display multiple images.

See this answer for a better way to form an image file filter (than hard coding types).

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433