Salvando aquivo em JFilechosser

4 respostas
leohunther

Fala gente boa!
Estou com um problema, para um projeto que estou fazendo em PDF.
Acontece que tenho um caminho pre determinado como um diretório.
Preciso fazer um aplicativo java para pegar esse aquivo abrir um JFileChooser e escolher um lugar para salvar o aquivo.
Só conheço o JFilechooser mas nunca usei. Então se puderem dar uma luz no código?! : )
Como se eu estivesse baixando um aquivo, mas simplismente de outra máquina ou da mesma máquina.
Poderia fazer um copy (.bat) pra isso, mas é pra usuário leigo, e querem uma janelinha pra ver onde o aquivo foi salvo. Alguém me ajude!!!
Muito grato a qualquer um!

4 Respostas

leohunther

jfilechooser.

leohunther

Alguém pode me ajudar, estou ficando desesperado.

ramilani12

Aqui tem varias dicas do JFileChosser
http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html

B

Olá leohunther,

Fiz um exemplo usando o JFileChooser para selecionar uma pasta e salvar um novo arquivo dentro dela…
espero que te ajude …
Att

import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.io.File;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import teste01.Main;
import teste01.Main;

public class Main extends JFrame {
    
    JLabel lbl;
    JTextField txt;
    JButton btn;
    
    public Main () {
        super("Teste");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400,400);
        setLayout(new FlowLayout());
        
        lbl = new JLabel("Nome do arquivo: ");
        add(lbl);
        txt = new JTextField(10);
        add(txt);
        btn = new JButton("Selecionar Pasta");
        Evento x = new Evento();
        btn.addActionListener(x);
        add(btn);
    }
    
    public static void main(String[] args) {
        Main starter = new Main();
        starter.setVisible(true);
        
    }
    
    private class Evento implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == btn) {
                JFileChooser fc = new JFileChooser();
                fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                int returnVal = fc.showOpenDialog(null);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File diretorio = fc.getSelectedFile();
                    File novoArquivo = new File(diretorio.getAbsolutePath() + "\\" + txt.getText());
                    try {
                        if (novoArquivo.createNewFile())
                            JOptionPane.showMessageDialog(null,"Arquivo criado em: " + novoArquivo.getAbsolutePath());
                        else
                            JOptionPane.showMessageDialog(null,"Erro na criação do arquivo...\nVerifique o nome do arquivo e o diretorio selecionado...");
                    } catch (HeadlessException ex) {
                        ex.printStackTrace();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        }
    }

}
Criado 20 de janeiro de 2008
Ultima resposta 21 de jan. de 2008
Respostas 4
Participantes 3