0

ここに画像の説明を入力

JavaでのUIデザインは初めてです。インターネットからファイルをダウンロードしてハード ドライブに保存するための GUI を作成しようとしています。追加したい1つのことを除いて、コードが機能しています。JFileChooserユーザーが宛先フォルダーを選択できるようにするを追加しました。filenameしかし、ユーザーがメニューのSave Asバーに入力したものに変更する方法がわかりません。JFileChooser

参照ボタン

browseButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
    chooser = new JFileChooser();
    chooser.setCurrentDirectory(null);
    chooser.setDialogTitle("Select folder to save");
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    chooser.setAcceptAllFileFilterUsed(true);

    //chooser.showDialog(downloadButton, "Save");
    if(chooser.showSaveDialog(downloadButton) == JFileChooser.APPROVE_OPTION)
    {
        System.out.println("The location to save is: " + chooser.getCurrentDirectory());
        DESTINATION_FOLDER = chooser.getCurrentDirectory().toString();
    }
}

});

ダウンロードボタン

URLConnection connection = downloadUrl.openConnection();

input = new BufferedInputStream(connection.getInputStream());
output = new FileOutputStream(DESTINATION_FOLDER + "/" + filename);

これfilenameは、ユーザーが入力するものです。これを行う方法についての指針はありますか?

4

3 に答える 3

1

Save As実際には、JFileChooserの Bar から FileName を取得する必要はありません。次のようにしてください:

    browseButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e){
        chooser = new JFileChooser();
        chooser.setCurrentDirectory(null);
        chooser.setDialogTitle("Select folder to save");
        //Don't use the 'FileSelectionMode();'. Let it be Default.
        chooser.setAcceptAllFileFilterUsed(true);
   if(chooser.showSaveDialog(downloadButton) == JFileChooser.APPROVE_OPTION)
   {
    file = chooser.getSelectedFile();
    //file should be declared as a File.
     System.out.println("The location to save is: " + chooser.getCurrentDirectory();));
     System.out.println("The FileName is: " + file.getName()); 
   }
} 

ダウンロードボタン:

   URLConnection connection = downloadUrl.openConnection();
   input = new BufferedInputStream(connection.getInputStream());
   output = new FileOutputStream(file);  
于 2012-04-13T19:09:37.540 に答える
0

あなたのコードをもっと見ることなく、私が提案できる最善の方法は、作業しているクラスにグローバル文字列を作成することです.

public class gui extends JFrame{
    public String filePath="";
    public static void main(String args[]){
        //button code
        browseButton.addActionListener(new ActionListener())
        saveAsButton.addActionListener(new ActionListener())
        URLConnection connection = downloadUrl.openConnection();



    }

public void actionPerformed(ActionEvent e)
{
    if (e.getActionCommand().equals("browseButton"){
        chooser = new JFileChooser();
        chooser.setCurrentDirectory(null);
        chooser.setDialogTitle("Select folder to save");
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        chooser.setAcceptAllFileFilterUsed(true);

        //chooser.showDialog(downloadButton, "Save");
        if(chooser.showSaveDialog(downloadButton) == JFileChooser.APPROVE_OPTION)
        {
            System.out.println("The location to save is: "+chooser.getCurrentDirectory());
            filePath = chooser.getCurrentDirectory().toString();
        }
    else{
        //save as button selected
        input = new BufferedInputStream(connection.getInputStream());
        output = new FileOutputStream(filePath);
    }
}

}
于 2012-04-12T17:17:54.430 に答える