2012-04-12 5 views
0

enter image description hereできないこと

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

ブラウズボタン

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ダウンロードボタンは、ユーザーが入力したものでなければなりません。これを行う方法の指針? 。私が提案することができる最高のは、あなたが作業しているクラスのグローバル文字列を作成することで、あなたのコードの多くを見ずに

答えて

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); 
    } 
} 

} 
+0

おかげ はちょうどこのように行います。しかし、実際、私は実際に、ユーザが選択した 'fileName'ではなく' fileName'をどのように使用するかを、選択メニューの 'Save As'テキストバーにどのように使うかを尋ねていました。私はより良い理解のために写真を提供しました。 – noMAD

0

は、この行を追加します。

chooser.setDialogType(JFileChooser.SAVE_DIALOG); 

完全なコードを:

browseButton.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) 
{ 
    chooser = new JFileChooser(); 
    chooser.setCurrentDirectory(null); 
    chooser.setDialogTitle("Select folder to save"); 
    chooser.setDialogType(JFileChooser.SAVE_DIALOG); 
    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(); 
    } 
} 
1

実際には、JFのSave AsバーからFileNameを取得する必要はありませんileChooser。

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); 
関連する問題