Jump to content

Dúvida sobre FileChooser


InformaticoTuga

Recommended Posts

        JButton btnUpload = new JButton("");
        btnUpload.setIcon(new ImageIcon(Form_Cliente.class.getResource("/icones/Icones90x40/Procurar2.png")));
        btnUpload.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                //JFileChooser FileChooser = new JFileChooser("C:/Users/SergioP/workspace/gtr/src/Imagens/clientes/");
                String dirInicio = (System.getProperty("user.dir") + "/src/Imagens/clientes/");
                System.out.println(dirInicio);
                JFileChooser FileChooser = new JFileChooser(dirInicio);
                FileNameExtensionFilter filter1 = new FileNameExtensionFilter("PNG","png"); 
                FileNameExtensionFilter filter2 = new FileNameExtensionFilter("GIF", "gif"); 
                FileNameExtensionFilter filter3 = new FileNameExtensionFilter("JPG", "jpg");

                FileChooser.addChoosableFileFilter(filter1);
                FileChooser.addChoosableFileFilter(filter2);
                FileChooser.addChoosableFileFilter(filter3);
                
                int returnValue = FileChooser.showOpenDialog(null);
                if (returnValue == JFileChooser.APPROVE_OPTION){
                    selectedFile = FileChooser.getSelectedFile();
                    
                    String fileName = selectedFile.getName();          
                    String fileExtension = fileName.substring(fileName.indexOf(".") + 1, selectedFile.getName().length());
                    System.out.println("File Extension: " + fileExtension);
                    if (!fileExtension.equals("png") && !fileExtension.equals("gif") && !fileExtension.equals("jpg")) {
                        
                        JOptionPane.showMessageDialog(null, "Erro ao Carregar Imagem!!\n" + "Formato não Suportado!!");
                        dispose();
                        new Form_Cliente("").setVisible(true);
                        
                    }else{
                    filePath = selectedFile.getAbsolutePath();
                    InputStream inStream = null;
                    OutputStream outStream = null;
                    try{
                        File source =new File(filePath);
                        File dest =new File(System.getProperty("user.dir") + "/src/Imagens/clientes/" + selectedFile.getName());
                        System.out.println(dest);
                      
                        inStream = new FileInputStream(source);
                        outStream = new FileOutputStream(dest);
                        
                        byte[] buffer = new byte[1024];
                        /*if(dest.delete()){
                            System.out.println(dest.getName() + " Was deleted!");
                        }else{
                            System.out.println("Delete Operation Failed. Check: " + dest);
                        }*/
                        int length;
                        while ((length = inStream.read(buffer)) > 0){
                            outStream.write(buffer, 0, length);
                        }
                        System.out.println(Foto.getText() + "-<"+fileName);
                     
                        JOptionPane.showMessageDialog(null, "A carregar...");
                        Thread.sleep(1000);
                        
                        RedimencionaImagemLabel(Foto, "clientes",fileName,90,90);
                        if (inStream != null)inStream.close();
                        if (outStream != null)outStream.close();
                        System.out.println("File Copied..");
                        JOptionPane.showMessageDialog(null, "Imagem Carregada com Sucesso!!\n" + "Imagem Carregada: " + selectedFile.getName());
                    }catch(IOException e1){
                        e1.printStackTrace();
                    } catch (InterruptedException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }}
        }});
        btnUpload.setFocusPainted(false);
        btnUpload.setBorder(null);
        btnUpload.setBackground((Color) null);
        btnUpload.setBounds(358, 122, 96, 34);
        Informacao.add(btnUpload);

Boa noite, eu estou a usar o filechooser num aplicativo que estou a desenvolver e o código acima funciona quando selecciono a imagem de uma outra pasta que não a clientes.

Mas quando selecciono uma imagem da pasta clientes, esta fica vazia e ilegível, eu penso que seja por este estar a escrever a imagem uma por cima da outra quando copia esta.

Visto que sou um novato no Java decidi de perdi ajudar aqui do pessoal mais especializado no assunto, se me puderem ajudar agradecia, Obrigado pelo vosso tempo.

Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.