0

enter image description here

I am new to UI design in Java. I am trying to create a GUI to download a file off the Internet and save it on your hard drive. I have got the code working except for one thing which I want to add. I have added a JFileChooser which lets the user select the destination folder. But I am unable to figure out how to change the filename to the one which user enters in the Save As bar on the JFileChooser menu.

Browse Button

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

});

Download Button

URLConnection connection = downloadUrl.openConnection();

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

Here filename should be the one which user enters. Pointers on how to get this done?

4

3 回答 3

1

实际上,您不需要从Save AsJFileChooser 的 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 回答
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();
    }
}
于 2012-04-12T17:52:02.870 回答